mexTcpBinding in WCF - IMetadataExchange errors - wcf

I'm wanting to get a WCF-over-TCP service working. I was having some problems with modifying my own project, so I thought I'd start with the "base" WCF template included in VS2008.
Here is the initial WCF App.config and when I run the service the WCF Test Client can work with it fine:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfTcpTest.Service1" behaviorConfiguration="WcfTcpTest.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/WcfTcpTest/Service1/" />
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="WcfTcpTest.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfTcpTest.Service1Behavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
This works perfectly, no issues at all.
I figured changing it from HTTP to TCP would be trivial: change the bindings to their TCP equivalents and remove the httpGetEnabled serviceMetadata element:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfTcpTest.Service1" behaviorConfiguration="WcfTcpTest.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:1337/Service1/" />
</baseAddresses>
</host>
<endpoint address="" binding="netTcpBinding" contract="WcfTcpTest.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfTcpTest.Service1Behavior">
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
But when I run this I get this error in the WCF Service Host:
System.InvalidOperationException: The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service Service1. Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract.
I get the feeling that you can't send metadata using TCP, but that's the case why is there a mexTcpBinding option?

Well, if you want to have metadata - TCP or HTTP - you still need to include the serviceMetadata behavior!
<behaviors>
<serviceBehaviors>
<behavior name="WcfTcpTest.Service1Behavior">
<serviceDebug includeExceptionDetailInFaults="True" />
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>
Sure, you can't have a "HttpGetEnabled" on it - but the behavior itself must be present in order to enable exchange of metadata (and thus the IMetadataExchange contract).

Related

WCF Web.Config - Duplicate Service Names Throwing Error

The following code is throwing the error
A child element named 'service' with same key already exists at the same configuration scope. Collection elements must be unique within the same configuration scope (e.g. the same application.config file). Duplicate key value: 'Marius.Marius_0_0_8'.
Now obviously there are two services named Marius.Marius_0_0_8. But when I try to change the name of one of them to something else I get a different error. I'm assuming that's because the name has to match the name of the namespace & class in the .svc.
I'm tired and have been staring at this for hours. What am I doing wrong? All I'm trying to really do is turn the Metadata on so I can hook up the WCF Test Client.
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
<customErrors mode="Off" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Marius.PublishTheMetaData" >
<!-- Add the following element to your service behavior configuration. -->
<serviceMetadata httpGetEnabled="true" policyVersion="Policy15" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Marius.MariusAspNetAjaxBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="Marius.Marius_0_0_8">
<endpoint address="" behaviorConfiguration="Marius.MariusAspNetAjaxBehavior"
binding="webHttpBinding" contract="Marius.Marius_0_0_8" />
</service>
<service name ="Marius.Marius_0_0_8" behaviorConfiguration="Marius.PublishTheMetaData">
<endpoint name="mex"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
</configuration>
You are trying to declare another service for the Mex endpoint. The correct way is to have only one service which exposes two endpoints: the real service endpoint and the wsdl endpoint.
<services>
<service name="Marius.Marius_0_0_8">
<endpoint address="" behaviorConfiguration="Marius.MariusAspNetAjaxBehavior"
binding="webHttpBinding" contract="Marius.Marius_0_0_8" />
<endpoint name="mex"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
Example: https://msdn.microsoft.com/en-us/library/ms734786(v=vs.110).aspx

Hosting WCF service

I am following THIS link to create my wcf service and hosting through window service its is working fine but when i change my namespace from "Microsoft.ServiceModel.Samples" to "EE.ServiceProcess.ABC" and do changes accordingly in app.config my window service dont get start properly
my
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="EE.ServiceProcess.ABC.CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/EEServiceProcessABCservice/service"/>
</baseAddresses>
</host>
<!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/EEServiceProcessABCservice/service -->
<endpoint address=""
binding="wsHttpBinding"
contract=" EE.ServiceProcess.ABC.ICalculator" />
<!-- the mex endpoint is explosed at http://localhost:8000/EEServiceProcessABCservice/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Can any one please suggest where am i going wrong?

WCF Default Endpoint Missing

I am deploying a WCF Service to IIS 7 and when I navigate to the service URL I get a page like this:
I do not get the typical landing page like this (copied from the web just to show example, data blurred to protect the innocent):
Can someone tell me what I am doing wrong? The service works as expected but there is nothing for a user of this service to view to know what the methods are or where to get the WSDL.
Here are my relevant files:
App.Config (in service project and copied to service site as Web.Config for IIS)
<system.web>
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<services>
<service name="MyService.MyService" behaviorConfiguration="Web">
<clear />
<endpoint address=""
binding="wsHttpBinding"
name="ws"
behaviorConfiguration="Web"
contract="MyService.IMyService" />
<!--
<endpoint address=""
behaviorConfiguration="Web"
binding="webHttpBinding"
bindingConfiguration=""
name="web"
contract="MyService.IMyService" />
-->
<host>
<baseAddresses>
<add baseAddress="http://services.mydomain.com/MyService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Web">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
MyService.svc
<%# ServiceHost Service="MyService.MyService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
I also tried using 'webHttpBinding' instead of 'wsHttpBinding' and it didn't seem to make a difference.
ServiceHost Service="MyService.MyService" ... is this line correct?

what is the "behaviorConfiguration" attribute of service?

what is the "behaviorConfiguration" attribute of service?
<services>
<service name="WcfServiceNetMSMQ.Service1" behaviorConfiguration="WcfServiceNetMSMQ.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8010/WcfServiceNetMSMQ/Service1/" />
</baseAddresses>
</host>
<endpoint address ="net.msmq://localhost/private/myqueue" binding="netMsmqBinding" contract="WcfServiceNetMSMQ.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
There are 3 important sections when you configure a WCF service.
1) Defining the Services:
<services>
<service behaviorConfiguration="SOAPRESTDemoBehavior" name="SOAPRESTDemo">
<endpoint address="rest" behaviorConfiguration="SOAPRESTDemoEndpointBehavior" binding="webHttpBinding" contract="ISOAPRESTDemo" />
<endpoint address="soap" binding="basicHttpBinding" contract="ISOAPRESTDemo" />
</service>
</services>
NOTE the value of behaviorConfiguration is a reference to a section further on in the config see below...
2) Defining the 'Service Behaviours'
<serviceBehaviors>
<behavior name="SOAPRESTDemoBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
3) Defining the 'Endpoint Behaviours'
<endpointBehaviors>
<behavior name="SOAPRESTDemoEndpointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
All 3 sections are the basics for what you need to set up a service (although this can be done programatically).
With regard to your question the behaviorConfiguration section relates to point 2 and 3 in my points above. Its where you lay out the sort of actions you want your service to have. for example above I have said that I want to allow MetaData to be published. This will essentially create a WSDL which describes the service.
The full config is here:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<!--Set up the service-->
<services>
<service behaviorConfiguration="SOAPRESTDemoBehavior" name="SOAPRESTDemo">
<endpoint address="rest" behaviorConfiguration="SOAPRESTDemoEndpointBehavior" binding="webHttpBinding" contract="ISOAPRESTDemo" />
<endpoint address="soap" binding="basicHttpBinding" contract="ISOAPRESTDemo" />
</service>
</services>
<!--Define the behaviours-->
<behaviors>
<serviceBehaviors>
<behavior name="SOAPRESTDemoBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="SOAPRESTDemoEndpointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
It is a reference to another configuration section:
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceNetMSMQ.Service1Behavior">
</behaviors>
</serviceBehaviors>
</behaviors>
Where this section contains some global configuration for the whole service.
here's an example of it, it carries some properties of the connection of the service.
<serviceBehaviors>
<behavior name="WcfServiceNetMSMQ.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
Notice that the name in this service behavior corresponds to the one you specified earlier.
Hope that helps.
You will get this error with Behavior name is not setup correctly.
The HTML document does not contain Web service discovery information.
Metadata contains a reference that cannot be resolved: 'http://blabla.com/WebService/Processor.svc'.
Content Type application/soap+xml; charset=utf-8 was not supported by service 'http://blabla.com/WebService/Processor.svc'. The client and service bindings may be mismatched.
The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..
If the service is defined in the current solution, try building the solution and adding the service reference again.

How can I use IMetadataExchange endpoint to get information about service metadata?

How can I use IMetadataExchange endpoint to get information about service metadata?
Do I need to implement it in a class like we do for other interfaces ?
Please help me ...
The framework will take care of the implementation for you.
However, you must create a service behavior that enables this.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<!-- behavior that enables metadata exchanges -->
<behavior name="mexGet">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<!-- assign the custom behavior here -->
<service name="MyNamespace.MyImplementation"
behaviorConfiguration="mexGet">
<host>
<baseAddresses>
<add baseAddress="http://localhost:3849"/>
</baseAddresses>
</host>
<endpoint
address="MyService"
binding="wsHttpBinding"
contract="MyNamespace.MyServiceInterface"
>
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange">
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
No, just add the endpoint that exposes IMetadataExchange. You don't have to implement anything.
No need to implement 'IMetadataExchange'. Just add endpoint:
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
One important thing, you must declare serviceMetadata, or your IMetadataExchange will not be found:
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>