Hosting a Simple Wcf Service in Console

后端 未结 1 1904
暖寄归人
暖寄归人 2020-12-29 00:45

I am trying to create a simple ConsoleApplication in which i would like to host a simple wcf service.

Here is the code for my

namespace HostConsoleA         


        
相关标签:
1条回答
  • 2020-12-29 01:12

    Well, I think the problem is this:

    • you have a base address for net.tcp
    • you have a MEX http endpoint defined (but no http base address)

    Basically if you want to use MEX over http, you need to supply either a full address for the MEX endpoint, or a http base address (if you only specify a relative address).

    Solution 1: specify a full address for the MEX endpoint:

     <services>
        <service name="FirstWcfService.Service" 
                  behaviorConfiguration="ServiceBehavior">
           <endpoint 
               address="FirstWcfService" 
               binding="netTcpBinding" 
               contract="FirstWcfService.IService"/>
           <endpoint 
               address="http://localhost:9102/FirstWcfService/mex"
               binding="mexHttpBinding" 
               contract="IMetadataExchange"  />
            ......
        </service>
    </services>
    

    Solution 2: define an HTTP base address, too:

     <services>
        <service name="FirstWcfService.Service" 
                  behaviorConfiguration="ServiceBehavior">
           <endpoint 
               address="FirstWcfService" 
               binding="netTcpBinding" 
               contract="FirstWcfService.IService"/>
           <endpoint 
               address="mex"
               binding="mexHttpBinding" 
               contract="IMetadataExchange"  />
           <host>
               <baseAddresses>
                   <add baseAddress="net.tcp://localhost:9101/"/>
                   <add baseAddress="http://localhost:9102/"/>
               </baseAddresses>
           </host>
        </service>
    </services>
    

    Solution 3: use the mexTcpBinding instead

     <services>
        <service name="FirstWcfService.Service" 
                  behaviorConfiguration="ServiceBehavior">
           <endpoint 
               address="FirstWcfService" 
               binding="netTcpBinding" 
               contract="FirstWcfService.IService"/>
           <endpoint 
               address="mex"
               binding="mexTcpBinding" 
               contract="IMetadataExchange"  />
            ......
        </service>
    </services>
    

    Any of those three options should should solve it.

    A word of caution: I find it quite risky to call your service behavior configuration "ServiceBehavior"......

    <serviceBehaviors>
        <behavior name="ServiceBehavior" >
    

    My recommendation: call your first and default configuation just plain "Default" (or "DefaultBehavior")

    <serviceBehaviors>
        <behavior name="Default" >
    

    and only start giving out other names if you have multiple configurations.

    Calling this ServiceBehavior just seems to be asking for trouble some time later on.....

    0 讨论(0)
提交回复
热议问题