WCF hosting issue within Windows service

2019-07-21 17:37发布

问题:

I have created a REST WCF service.

First i have hosted in Console app, it was working OK.

Then i have hosted it in Windows service. It is throwing various errors

Following is the config

<system.serviceModel>
<diagnostics>
  <messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
</diagnostics>
<behaviors>
  <serviceBehaviors>
    <behavior name="serviceMetadataBehavior">
      <serviceMetadata />
    </behavior>
    <behavior name="restServiceMetaBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>

  </serviceBehaviors>

  <endpointBehaviors>
    <behavior name="jsonEndpointBehavior">
      <enableWebScript />
    </behavior>
    <behavior name="wsHttpBinding" />
  </endpointBehaviors>
</behaviors>

<services>

<service behaviorConfiguration="restServiceMetaBehavior" name="StockPriceNotifierServiceImpl.StockNotifierServiceImpl">
<!--SOAP endpoint-->
  <endpoint address="net.tcp://localhost:8082/StockPriceService"
      binding="netTcpBinding" bindingConfiguration="" contract="StockPriceNotifierService.IStockNotifierService" />
  <endpoint address="net.tcp://localhost:8082/StockPriceService/mex" binding="mexTcpBinding" bindingConfiguration=""  contract="IMetadataExchange"/>


  <!--rest endpoint-->

  <endpoint address="http://localhost:8084/StockNotifierService"
      binding="webHttpBinding"  name="webhttp"
      contract="StockPriceNotifierService.IStockNotifierRestService" />
    <endpoint address="http://localhost:8084/StockNotifierService/mex"
     binding="mexHttpBinding" name="MexWebHttp" contract="IMetadataExchange" />
  </service>
</services>

I have tried to troubleshoot but no success.

Following are the steps taken so far

  1. Inital error -> The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the HttpGetUrl property is a relative address, but there is no http base address. Either supply an http base address or set HttpGetUrl to an absolute address. Resolution -> http://social.msdn.microsoft.com/Forums/pl/wcf/thread/2fd858cb-8988-444f-868c-2ceea9cc9705, Specified HttpGetUrl

  2. AddressFilter mismatch error ->The message with To 'http://localhost:8084/StockService/MSFT/Price' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree. Resolution -> Added behavior ->[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]

  3. Now ContractFilter mismatch exception -> System.ServiceModel.FaultException: The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher.

Seems like i am missing some basics, but no clue so far.

Contracts

 [ServiceContract] //REST
  public interface IStockNotifierRestService
  {
    [OperationContract]
    [WebGet(UriTemplate = Routing.GetCurrentPriceRoute,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Json
        )]
    double GetCurrentPrice(string symbol);



 [ServiceContract]  //SOAP
 public interface IStockNotifierService:IStockNotifierRestService
  {
    [OperationContract]
    double GetPrice(String symbolname, DateTime timestamp);
  }

Implementation

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, 
        InstanceContextMode= InstanceContextMode.Single, AddressFilterMode=AddressFilterMode.Any)]

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class StockNotifierServiceImpl:IStockNotifierService
    {


        public double GetPrice(string symbolname, DateTime timestamp)
        {
            return 12.0;
        }

        public double GetCurrentPrice(string symbolname)
        {
            return 13.0;
        }
   }
标签: .net wcf rest