How to invoke WCF RESTful Service in Windows Service in asp.net?

主宰稳场 提交于 2019-12-11 07:12:09

问题


I have created below Operation Contract for POST Method in WCF RESTfule Service.

IService1.cs:-

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "/SaveCustomerPost",
 BodyStyle = WebMessageBodyStyle.Wrapped)]
  string SaveCustomerDetails(CustomerDetails objCustomerDetails);  

[DataContract]
public class CustomerDetails
{
  [DataMember]
  public string Name { get; set; }      
}

Windows Service:-

using (WebChannelFactory<ServiceReference1.IService1> cf = new WebChannelFactory<ServiceReference1.IService1>(new Uri("http://xxx/CustomerService.svc/SaveCustomerPost")))
{
   var helloService = cf.CreateChannel();
   ServiceReference1.CustomerDetails objCustomerDetails = new ServiceReference1.PANNoDetails();
   objPANNoDetails.Name = "TestName";           
   string strResult = helloService.SaveCustomerDetails(objPANNoDetails);
}

Client App.config:-

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="CustBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="WebHttpBinding" sendTimeout="00:05:00" maxBufferSize="2147483647"
          maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
          transferMode="Streamed">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
    <client>
      <endpoint address=""
          binding="webHttpBinding" behaviorConfiguration="CustBehavior"
          contract="ServiceReference1.ICustService" name="WebHttpBinding" />
    </client>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0"/>
  </system.serviceModel>

There was no endpoint listening at "xxx.svc/SaveCustomerDetails" that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

When I invoke above mentioned method with WebInvoke method in window service I got above mentioned error. When I invoke above mentioned service without WebInvoke method, service is working fine. How to resolve above mentioned issue?


回答1:


When creating the ChannelFactory, try to specify the service name only:

using (ChannelFactory<ServiceReference1.ICustService> factory = new ChannelFactory<ServiceReference1.ICustService>(
    new WebHttpBinding(),
    "http://xxx/CustomerService.svc"))
{
}


来源:https://stackoverflow.com/questions/41570569/how-to-invoke-wcf-restful-service-in-windows-service-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!