简体   繁体   中英

Hosting a WCF service over TCP | Structuring Project

I'm trying to develop a WCF service in the following manner:

  • A class library project which will have all contract and their implementations , called WcfContracts.

  • A WCF service library project which will have the configuration part ie it will have only the app.config. The project will have references to the class library which is having the contract & their implementations.ie to WcfContracts.This project is called WcfServiceHosting.

  • A windows service which can host the WCF service. The service will have the same app.config as the WCF service and will be used only for hosting the WCF service.I created a new app.config in the service project, and copied the contents of the app.config from WCF service library to it.

  • A WPF client application, which was intended to talk to the service hosted in the windows service. I was adding ServiceReference to the WCF contract.

  • All projects lie in the same solution.

However while doing the above I'm not able to add the service reference in the client project. If I keep the contracts & implementations in the same WCF service library everything works fine.

The following is my app.config for WCF service library:

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

  <system.web>
    <compilation debug="true" />
  </system.web>

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="WcfServiceHosting.Service1Behavior"
        name="WcfServiceHosting.CalculatorService">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="**WcfServiceHosting.ICalculatorService**">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8523/CalculatorService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfServiceHosting.Service1Behavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

Now the above works just fine because IcalculatorService and CalculatorService resides in the same assembly ie WcfServiceHosting.

Now if I remove the ICalculatService and CalculatorService from WcfServiceHosting and add it to WcfContracts, I'm not able to add the service reference. It does not show up in the reference dialog while discovering.

The following is my modified contract:

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

  <system.web>
    <compilation debug="true" />
  </system.web>

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="WcfServiceHosting.Service1Behavior"
        name="**WcfContracts.CalculatorService**">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WcfContracts.ICalculatorService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8523/CalculatorService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfServiceHosting.Service1Behavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

The above does not work, can you please tell me why? I have added the necessary references and all.

Thanks, - Mike

Add Service Reference won't be able to see any contracts that aren't hosted. You need to reference the contracts (or contain them in) a project that hosts the WCF service.

eg how would add references know how your CalculatorServce should be hosted (TCP, HTTP, NamedPipes, MSMQ) if a host wasn't there to include the config?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM