locked
Console App calls WCF service RRS feed

  • Question

  • User1453549677 posted

    I am trying to see what my ConsoleApplication code should look like to issue the post. I made an attempt. Can anyone help?

    ++++++++++++++++++ IRESTServiceImpl.cs +++++++++++++++++++++++++

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;

    namespace RESTService
    {
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRestServiceImpl" in both code and config file together.
    [ServiceContract]
    public interface IRestServiceImpl
    {
    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Xml,
    RequestFormat = WebMessageFormat.Xml,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "auth")]
    ResponseData Auth(RequestData rData);

    }
    [DataContract(Namespace = "http://www.eysnap.com/mPlayer")]
    public class RequestData
    {
    [DataMember]
    public string details { get; set; }
    }
    public class ResponseData
    {
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Age { get; set; }

    [DataMember]
    public string Exp { get; set; }

    [DataMember]
    public string Technology { get; set; }
    }
    }

    +++++++++++++++++++++++++++++++++++++ RESTServiceImpl.svc.cs +++++++++++++++++++++++++++++++++

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;

    namespace RESTService
    {
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "RestServiceImpl" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select RestServiceImpl.svc or RestServiceImpl.svc.cs at the Solution Explorer and start debugging.
    public class RestServiceImpl : IRestServiceImpl
    {
    public ResponseData Auth(RequestData rData)
    {
    //Call BLL here
    var data = rData.details.Split('|');
    var response = new ResponseData
    {
    Name = data[0],
    Age = data[1],
    Exp = data[2],
    Technology = data[3]
    };
    return response;
    }
    }
    }

    +++++++++++++++++++++++++++++++++++++++++++++++++ web.config ++++++++++++++++++++++++++++++++

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>

    <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpModules>
    <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
    </system.web>
    <system.serviceModel>
    <services>
    <service name= "RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
    <endpoint address="" binding ="webHttpBinding" contract="RestService.RestServiceImpl" behaviorConfiguration="web">
    </endpoint>
    </service>
    </services>
    <behaviors>
    <serviceBehaviors>
    <behavior name="ServiceBehaviour">
    <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
    <serviceMetadata httpGetEnabled="true" />
    <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
    <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
    <behavior name="web">
    <webHttp/>
    </behavior>
    </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
    <remove name="ApplicationInsightsWebTracking" />
    <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
    <!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
    -->
    <directoryBrowse enabled="true" />
    <validation validateIntegratedModeConfiguration="false" />
    </system.webServer>

    <runtime>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

    <dependentAssembly>

    <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />

    <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />

    </dependentAssembly>

    <dependentAssembly>

    <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />

    <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />

    </dependentAssembly>

    </assemblyBinding>

    </runtime>
    </configuration>

    ++++++++++++++++++++++++++++++++++++ Console Application +++++++++++++++++++++++++++++

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    string data = "auth";
    string url = "http://localhost/RESTService/RestServiceImpl.svc/" + data;
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";
    req.ContentType = "text/xml; charset=utf-8";
    req.ContentLength = 0;
    System.Net.WebResponse resp = req.GetResponse();
    using (Stream stream = resp.GetResponseStream())
    {
    using (StreamReader reader = new StreamReader(stream))
    {
    Console.WriteLine(reader.ReadToEnd());
    }
    }
    Console.ReadLine();
    }
    }
    }

    Thursday, February 15, 2018 9:45 PM

Answers

  • User475983607 posted

    Don't use WCF for REST.  While it is possible, it is not the right tool for the job and it is very complex.  Rather use Web API as that's what Web API is for.  

    The issue with the console app is you are appending data to the end of the URL but issuing a POST.  In HTTP, The GET method sends data in the URL.  The POST sends data in the message body.  Basically, the design is wrong as you are not using HTTP properly.

    Again, you should use the right tool for the job.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 16, 2018 12:15 PM