Hosting a simple Wcf service in the console

I am trying to create a simple console application in which I would like to host a simple wcf service.

Here is the code for my

namespace HostConsoleApplication { class Program { static void Main(string[] args) { using (System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(FirstWcfService.Service))) { host.Open(); Console.WriteLine("Sai"); Console.ReadLine(); } } } } 

Then I added app.config, which looks like this:

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="FirstWcfService.Service" behaviorConfiguration="ServiceBehavior"> <endpoint address="FirstWcfService" binding="netTcpBinding" contract="FirstWcfService.IService"/> <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> <host> <baseAddresses> <add baseAddress="net.tcp://localhost:9101/"/> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior" > <serviceMetadata httpGetEnabled="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> 

When I run the host console application, I get this exception:

System.InvalidOperationException unhandled Message = "Could not find a base address that matches the http scheme for the endpoint with the MetadataExchangeHttpBinding binding. Registered base address schemes: [Net.tcp]."
Source = "System.ServiceModel"
Stack Traces: in System.ServiceModel.ServiceHostBase.MakeAbsoluteUri (Uri relativeOrAbsoluteUri, Binding Binding, UriSchemeKeyedCollection baseAddresses) in System.ServiceModel.Description.ConfigLodeaderElementE ServiceBementE ServiceDescriptionElement ServiceEmemente ServiceDemente ServiceDemente ServiceDemente ServiceDemente ServiceDemente ServiceDemente ServiceDemente ServiceDemente ServiceHost .ServiceHostBase.LoadConfigurationSectionInternal (ConfigLoader configLoader, ServiceDescription description, ServiceElement serviceSection) in System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal (ConfigLoader configLoader.ServiceLvice.ServiceIserviceServiceService.Service_Service_Service_Service). (UriSchemeKeyedCollection baseAddresses) in System.ServiceModel.ServiceHost.InitializeDescription (Type serviceType, Ur iSchemeKeyedCollection baseAddresses) in System.ServiceModel.ServiceHost..ctor (type serviceType, Uri [] baseAddresses) in HostConsoleApplication.Program.Main (String [] args) in C: \ Documents and Settings \ navin.pathuru \ My Documents \ Visual Studio 2008 \ Projects \ Solution2 \ HostConsoleApplication \ Program.cs: lines 13 in System.AppDomain._nExecuteAssembly (assembly assembly, String [] args) in System.AppDomain.ExecuteAssembly (String assembly, proof assemblySecurity, String [] args) in Microsoft. VisualStudio.HostingProcess.HostProc.RunUsersAssembly () in System.Threading.ThreadHelper.ThreadStart_Context (Object state) in System.Threading.ExecutionContext.Run (ExecutionContext executeContext, ContextCallback callback, object state) in System.Threading.Threadel.Threadel.Threadel.Threadel.Threadel.ThreadelThThread InnerException:

Just wondering how to fix it. thanks N

+7
console wcf
source share
1 answer

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 address)

Basically, if you want to use MEX via http, you need to specify the full address for the MEX endpoint or the base address of http (if you specify only the relative address).

Solution 1: Indicate the full address of 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: also determines the base address of the HTTP:

  <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 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 these three options should solve this problem.

Warning: it’s very risky for me to call the configuration of your ServiceBehavior ......

 <serviceBehaviors> <behavior name="ServiceBehavior" > 

My recommendation: first call your first and standard configuration "Default" (or "DefaultBehavior")

 <serviceBehaviors> <behavior name="Default" > 

and just start issuing other names if you have several configurations.

Calling this ServiceBehavior just seems to be asking for a problem after a while .....

+15
source share

All Articles