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

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>

Related

WCF IMetadataExchange Error

I have created a service host with following configuration.
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="MathServiceLib.MathService" behaviorConfiguration="myMathServiceBehave">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/MathService"/>
<add baseAddress="net.tcp://localhost:9002/MathService"/>
</baseAddresses>
</host>
<endpoint address="http://localhost:9001/MathService" binding="basicHttpBinding" contract="MathServiceLib.IMathService"/>
<endpoint address ="net.tcp://localhost:9002/MathService" binding ="netTcpBinding" contract="MathServiceLib.IMathService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<!--<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>-->
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="myMathServiceBehave">
<!--<serviceMetadata httpGetEnabled="true"/>-->
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
I don't want to use serviceMetadata httpGetEnabled="true" property instead I want to use MEX binding for proxy creation.
Now when I start my service using
svcHost = new ServiceHost(typeof(MathServiceLib.MathService));
svcHost.Open();
I get following error, please help.
The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service MathService. Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract.
Based on my experience, you need to specify
<serviceMetadata httpGetEnabled="true"/>
if you want to use
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
The following articles provide comprehensive information and examples:
http://msdn.microsoft.com/en-us/library/ms734765(v=vs.110).aspx
http://www.c-sharpcorner.com/UploadFile/81a718/wcf-service-faqs-part-2/

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 cannot configure WSDL publishing

I want to enable WSDL retrieval for a webservice that I created. I can 'call' the webservice through the .svc file that I made:
http://localhost/test.svc
<%# ServiceHost Language="C#" Debug="true" Service="Project.MyService" CodeBehind="MyService.svc.cs" %>
Calling the page gives the standard .NET message saying "Metadata publishing for this service is currently disabled." with instructions to enable publishing.
I followed instructions and added the web.config entries, however calling http://localhost/test.svc?wsdl produces the same result... How can I enable WSDL publishing for my webservice?
web.config entries
<service name="Project.IMyService" behaviorConfiguration="MyServiceTypeBehaviors" >
<endpoint contract="IMetadataExchange" binding="mexHttpBinding"
address="mex"
/>
<host>
<baseAddresses>
<add baseAddress="http://localhost/test.svc"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
Sounds crazy but just remove behaviorConfiguration="MyServiceTypeBehaviors" from the service definition andlet the behavior anonymous (with no name). It will remain
<services>
<service name="WcfServiceLibrary1.IMyService" >
<endpoint contract="IMetadataExchange" binding="mexHttpBinding"
address="mex"
/>
<host>
<baseAddresses>
<add baseAddress="http://localhost/test.svc"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
The mex endpoint and httpGetEnabled look ok. This link covers the topic in some more detail and may be of use.
http://msdn.microsoft.com/en-us/library/ms788760.aspx

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.

mexTcpBinding in WCF - IMetadataExchange errors

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