Tuesday, February 20, 2007

VB WCF template in Visual Studio 2005 extensions for .NET Framework 3.0

Just an FYI to anyone that is playing with WCF 2005 extensions in Visual Basic. When you create a WCF Service Library in VB, the code for the DataContract1 class it provides does not have <DataMember()> attributes on the properties FirstName and LastName, so when you try to generate a proxy it doesn't generate correctly and the sample doesn't work.

New Project --> pick template VB/.NET Framework 3.0/WCF Service Library

You'll see that the code for the DataContract1 at the bottom of the file doesn't have any <DataMember()> attributes. This has been fixed in the Orcas CTP that's due out very soon. So for now just pop those attributes in there and then follow the rest of the directions in the template to set up a host and config files.

<DataContract()> _
Public Class DataContract1
    Private m_firstName As String
    Private m_lastName As String

    <DataMember()> _
    Public Property FirstName() As String
        Get
            Return m_firstName
        End Get
        Set(ByVal value As String)
            m_firstName = value
        End Set
    End Property

    <DataMember()> _
    Public Property LastName() As String
        Get
            Return m_lastName
        End Get
        Set(ByVal value As String)
            m_lastName = value
        End Set
    End Property
End Class

The template doesn't tell you directly how to set up a client to call it, but there is a VB example that shows you how. It's provided in the Program Files\Microsoft SDKs\Windows\6.0\Samples\ then extract the WCFSamples.zip and locate the \TechnologySamples\Basic\GettingStarted\VB\GetingStarted.sln. Here you can see a sample client and how to add the proper MetaData Exchange (mex) endpoint and service behavior so that you can generate the service proxy on the client side using "Add Service Reference" from the project menu.

If you're following the instructions in the template, to add the endpoint to the host console application you need to put this in your app.config (note, if your host is a web server then you will place this in the web.config):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
 <services>
  <service 
   name="WCFServiceLibrary1.Service1"
   behaviorConfiguration="metadataSupport">
   <host>

<!-- Stick this in here if you remove the baseAddress 
        in the StartService method it creates for you. It's a 
        good idea to change that to use this config file instead.

 <baseAddresses>
   <add baseAddress="http://localhost:8080/ConsoleApplicationVB/service1" />
 </baseAddresses>
-->

   </host>
   <endpoint
    address=""
    binding="wsHttpBinding"
    contract="WCFServiceLibrary1.IService1" />

<!-- Adds a WS-MetadataExchange endpoint at 
        "http://localhost:8080/SampleService/mex" -->
   <endpoint
    address="mex"
    binding="mexHttpBinding"
    contract="IMetadataExchange" />
  </service>
 </services>
 <behaviors>
  <serviceBehaviors>
 <behavior name="metadataSupport">

<!-- Enables the IMetadataExchange endpoint in services that
        use "metadataSupport" in their behaviorConfiguration attribute.
        In addition, the httpGetEnabled and httpGetUrl attributes publish 
        Service metadata for retrieval by HTTP/GET at the address 
        "http://localhost:8080/SampleService?wsdl" -->

  <serviceMetadata httpGetEnabled="true" />

 </behavior>
  </serviceBehaviors>
 </behaviors>
</system.serviceModel>
</configuration>

Then start the service (this is important) and in your client select from the project menu "Add Service Reference" and put the MetaData Exchange endpoint as the service URI and it will generate the client code for you with no problems.

1 comment:

Anonymous said...

Thanks for the nice post!