Dennis van der Stelt

The only way to win is to learn faster than anyone else

Community

Email Notifications

News

  • Addicted to Refactor! Pro

I read...

I Use...

Tags

Recent Posts

Archives

Blog Subscription Form

  • Email Notifications
    Go

WCF Part 4 : Make your service visible through metadata

Last time we saw how we could create an instance of our service by hosting it using some configuration in our app.config. We still need to have it exposed using metadata though. We'll do this by adding an endpoint that exposed this, using our WCF ABC again. This endpoint is called a MEX endpoint, from Metadata EXchange.

For this we don't have to create any code, just configuration again. Open the Service Configuration Editor again on our app.config. Open the folder "Advanced", then "Service Behaviors" and choose to add a new service behavior. We'll change the name NewBehavior to HelloServiceBehavior. Now click the Add button and select the 'ServiceMetadata' option.

Our added service behavior for the MEX endpoint.

Now we'll configure our new behavior in and endpoint. Select your service again in the tree. This is something you'll probably forget a lot in the future, but you'll have to bind the just configured behavior to your service. You should be able to select it from the list in the BehaviorConfiguration property on your service.

Now let's actually add our MEX endpoint. Select the "Endpoints" folder under our service and right-click it, then choose to add a new endpoint. Set the address to http://localhost:8080/HelloService/MEX/, select for binding the "mexHttpBinding" and for contract fill in "IMetadataExchange". Now save, exit the configuration editor and you should have the following configuration.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="HelloServiceBehavior">
                    <serviceMetadata />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="HelloServiceBehavior" name="Classa.Wcf.Samples.Hello">
                <endpoint address="http://localhost:8080/HelloService/" binding="basicHttpBinding"
                    bindingConfiguration="" contract="Classa.Wcf.Samples.IHello" />
                <endpoint address="http://localhost:8080/HelloService/MEX/" binding="mexHttpBinding" bindingConfiguration=""
                    contract="IMetadataExchange" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

Run your service with F5. You'll notice no difference, but now you can create a proxy with the service util. Open the Visual Studio Command Prompt (in your start menu under Visual Studio 2005/Visual Studio Tools) and execute the following command:

svcutil.exe /o:client.cs /config:app.config http://localhost:8080/HelloService/MEX/

If you're interested, take a look at the generated files. Especially the generated app.config, which should contain an ABC reference to your service.

[Go to the WCF series article index]

Comments

Community Blogs said:

Dennis van der Stelt posted some fine articles about Microsoft's new way of doing communication between

# November 10, 2006 2:49 AM

Jan Schreuder said:

You may be pleased to know that Microsoft will release a service factory for WCF services next month!!!

# November 13, 2006 1:56 AM

Dennis van der Stelt said:

I'm not sure I care... From what I've seen so far, the service factory isn't my kind of thing.

You made a post that at TechEd they had a service up and running in 10 minutes. I can do this by hand in under 30 seconds!

# November 13, 2006 2:06 AM

Dennis' Blog said:

Last time we generated the client and configuration file. Whereas in the asmx world we had a proxy class,

# November 16, 2006 2:11 PM

Jean-Paul Smit said:

What is the extra you get by adding an 'mex' endpoint?

I noticed you can generate a client by just adding a behaviour and have the 'httpgetenabled' set to true.

Can you tell a bit more about what the mex is for?

# January 16, 2007 1:19 PM

Dennis van der Stelt said:

The Metadata behavior will generate your WSDL and XSD. It kind of crawls over your contracts, looks at them and generates these xml documents.

When you want to provide this through HTTP you must specifiy HttpGetEnabled. But when you use the service-util (svcutil.exe) you don't need the http-get.

In the old days (before the June 2006 Beta) the MEX endpoint was added automatically. I figure about 80% of the demo-code available on the internet, doesn't comply anymore to the MEX endpoints you now have to add manually. :)

Anyway, MEX endpoints are more than http-gets, you can also support https, tcp, etc. Where in the asmx days, you always had your WSDL via HTTP. You can also choose to NOT have a MEX endpoint and provide your customers with a WSDL and XSD's of your own.

# January 16, 2007 2:52 PM

Dennis van der Stelt said:

It’s been a long, long time since I wrote the original WCF Simple Example post. It was even before Visual

# June 16, 2010 8:40 PM

Michael said:

Okay, so this is all well and good. I take it the examples are done using a full version of Visual Studio? What do you do if you're trying to walk through these examples and all you've got is Visual C# Express 2010?

# July 9, 2010 6:32 PM

Dennis van der Stelt said:

@Michael : As far as I know, should work exactly the same. The only difference is that you don't have the team building stuff and some advanced remote debugging of assemblies in Express, plus no AddOn support. Should work exactly the same for everything else.

# July 9, 2010 10:53 PM

erum said:

can any one correct my service problem

<?xml version="1.0"?>

<configuration>

 <configSections>

   <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

     <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

       <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>

       <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

         <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />

         <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />

         <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />

         <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />

       </sectionGroup>

     </sectionGroup>

   </sectionGroup>

 </configSections>

 <appSettings/>

 <connectionStrings>

   <add name="VotingConnectionString" connectionString="Data Source=IRAM-PC;Initial Catalog=Voting;Integrated Security=True"

       providerName="System.Data.SqlClient" />

 </connectionStrings>

 <system.web>

   <!--

           Set compilation debug="true" to insert debugging            symbols into the compiled page. Because this            affects performance, set this value to true only            during development.        -->

   <compilation debug="false">

     <assemblies>

       <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

       <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

       <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

       <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

     </assemblies>

   </compilation>

   <!--

           The <authentication> section enables configuration            of the security authentication mode used by            ASP.NET to identify an incoming user.        -->

   <authentication mode="Windows" />

   <!--

           The <customErrors> section enables configuration            of what to do if/when an unhandled error occurs            during the execution of a request. Specifically,            it enables developers to configure html error pages            to be displayed in place of a error stack trace.        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">            <error statusCode="403" redirect="NoAccess.htm" />            <error statusCode="404" redirect="FileNotFound.htm" />        </customErrors>        -->

   <pages>

     <controls>

       <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

       <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

     </controls>

   </pages>

   <httpHandlers>

     <remove verb="*" path="*.asmx"/>

     <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

     <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

     <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>

   </httpHandlers>

   <httpModules>

     <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

   </httpModules>

 </system.web>

 <system.codedom>

   <compilers>

     <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"

               type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

       <providerOption name="CompilerVersion" value="v3.5"/>

       <providerOption name="WarnAsError" value="false"/>

     </compiler>

   </compilers>

 </system.codedom>

 <!--

       The system.webServer section is required for running ASP.NET AJAX under Internet        Information Services 7.0.  It is not necessary for previous version of IIS.    -->

 <system.webServer>

   <validation validateIntegratedModeConfiguration="false"/>

   <modules>

     <remove name="ScriptModule" />

     <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

   </modules>

   <handlers>

     <remove name="WebServiceHandlerFactory-Integrated"/>

     <remove name="ScriptHandlerFactory" />

     <remove name="ScriptHandlerFactoryAppServices" />

     <remove name="ScriptResource" />

     <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"

          type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

     <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode"

          type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

     <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

   </handlers>

 </system.webServer>

 <runtime>

   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

     <dependentAssembly>

       <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>

       <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>

     </dependentAssembly>

     <dependentAssembly>

       <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>

       <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>

     </dependentAssembly>

   </assemblyBinding>

 </runtime>

 <system.serviceModel>

   <behaviors>

     <endpointBehaviors>

       <behavior  name="VotingPanel2.Web.Service1AspNetAjaxBehavior">

         <enableWebScript />

       </behavior>

     </endpointBehaviors>

   </behaviors>

   <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

   <services>

     <service name="VotingPanel2.Web.Service1">

       <endpoint address="localhost/Service1" behaviorConfiguration="VotingPanel2.Web.Service1AspNetAjaxBehavior"

           binding="basicHttpBinding" contract="VotingPanel2.Web.Iservice1" />

           </service>

   </services>

 </system.serviceModel>

</configuration>

my email address is emirza_pk@hotmail.com

# September 16, 2010 1:15 PM

sam said:

hi dennis,

I have a problem! after following your steps the generated app.config file for service does not contain (service behaviorConfiguration="HelloServiceBehavior" )

and i add it by hand to app.config, where is my mistake?

# July 6, 2011 5:36 PM

Tung said:

I've created a service in which three bussiness endpoints are. I've also created a mex endpoint to describe them. But I want to show only two endpoints. So  How can I do that? Thanks.

# February 29, 2012 7:24 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 8 and 5 and type the answer here: