Announcement

All important changes of my site and my blog will be announced here.

Computer security

This is my hobby. I spend my freetime for it. I am not professional in this area. But I like it very much.

Everything Else

Which doesn’t belong to other categories will land here. It maybe about my daily life, entertainment, music, game … or something like that

Programming

This is my job. That means everyday I must sit before the monitor. Tip something, be annoyed by bugs,… but I simply like it.

Tutorial

When I found something interesting, I would like to share it to everyone. That is the reason for this category comes.

Home » Programming

WCF – Metadata Exchange Endpoint

Submitted by on Monday, 23 November 2009No Comment | 2,101 views

During my developing my tool for file transferring with TCP protocol in WCF. I met an annoyed error saying:

Metadata contains a reference that cannot be resolved: ‘net.tcp://localhost:7633/ClientTransferring/mex’.
Could not connect to net.tcp://localhost:7633/ClientTransferring/mex. The connection attempt lasted for a time span of 00:00:02.0117187. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:7633.
No connection could be made because the target machine actively refused it 127.0.0.1:7633
If the service is defined in the current solution, try building the solution and adding the service reference again.

As I read this error I thought that I forgot to add a service end point. In WCF, the service can also publish its metadata over a special endpoint called the metadata exchange endpoint, sometimes referred to as the MEX endpoint. That endpoint supports an industry standard for exchanging metadata, represented in WCF by the IMetadataExchange interface

[ServiceContract(...)]
public interface IMetadataExchange
{
   [OperationContract(...)]
   Message Get(Message request);
   //More members
}

The details of this interface are inconsequential. Like most of these industry standards, it is difficult to implement. Fortunately, WCF can have the service host automatically provide the implementation of IMetadataExchange and expose the metadata exchange endpoint. All you need to do is designate the address and the binding to use, as well as add the service metadata behavior. For the bindings, WCF provides dedicated binding transport elements for the HTTP, HTTPS, TCP, and IPC protocols. For the address, you can provide a full address or use any of the registered base addresses. There is no need to enable the HTTP-GET option, but there is no harm either.

Like any other endpoint, you can only add a metadata exchange endpoint programmatically before opening the host. WCF does not offer a dedicated binding type for the metadata exchange endpoint. Instead, you need to construct a custom binding that uses the matching transport binding element, and then provide that binding element as a construction parameter to an instance of a custom binding. Finally, call the AddServiceEndpoint( ) method of the host providing it with the address, the custom binding, and the IMetadataExchange contract type.

    ServiceMetadataBehavior smbBehavior = m_shHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smbBehavior == null)
                {
                    smbBehavior = new ServiceMetadataBehavior();
                    m_shHost.Description.Behaviors.Add(smbBehavior);
                }
                m_shHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "net.tcp://localhost:" + (CustomConfiguration.Port - 1).ToString() + CustomConfiguration.ServiceName + "/mex");
                m_shHost.Open();

But after adding this code I still could not solve this error. After 30 minutes of “cursing” Microsoft on this silly error I found out that I enter the wrong URL. The “transferring” word should be with 2 ‘r’ but in my source code I only tipped 1 ‘r’ and so I can not make a reference to it.

Popularity: 3% [?]

My strongly recommended books to read. Choose one and enjoy yourself.

Related Posts:

  • No Related Posts

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.