none
How to configure WCF Service settings using vb.net in WCF service constructor by overriding the constructor. RRS feed

  • Question

  • Dear All,

    I have a WCF service that has a App.config associated with it. The App.config file content is as below

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.web>
        <compilation debug="true" />
      </system.web>
      <!-- When deploying the service library project, the content of the config file must be added to the host's 
      app.config file. System.Configuration does not support config files for libraries. -->
      <system.serviceModel>
        <services>
          
          <service behaviorConfiguration="HelloworldWCFService.HelloWorldServiceBehavior"
            name="HelloworldWCFService.HelloWorldService">
            <endpoint address="" binding="wsHttpBinding" contract="HelloworldWCFService.IHelloWorldService">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8731/Design_Time_Addresses/HelloworldWCFService/HelloWorldService/" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="HelloworldWCFService.HelloWorldServiceBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>
    

    Please let me know how can I configure these above settings from the WCF Service constructor using VB.Net and remove the settings from App.config file. Any help is appreciated.

    regards,

    Noor


    Thanks & Regards, Noor Hussain

    Thursday, March 21, 2013 2:55 PM

Answers

  • Hi,

    It seems that you want to create a service which is self-hosted, you can create a Console application to host the service, and set the host app as StartUp Project. Below is the code that used to host the service.

    Sub Main()
            Dim svcHost As New ServiceHost(GetType(HelloWorldService.Service1
                                           ), New Uri("http://localhost/HelloWorldService"))
    
            'Check to see if the service host already has a ServiceMetadataBehavior
            Dim smb As ServiceMetadataBehavior = svcHost.Description.Behaviors.Find(Of ServiceMetadataBehavior)()
            'If not, add one
            If (smb Is Nothing) Then
                smb = New ServiceMetadataBehavior()
            End If
            smb.HttpGetEnabled = True
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15
            svcHost.Description.Behaviors.Add(smb)
            'Add MEX endpoint
            svcHost.AddServiceEndpoint( _
                  ServiceMetadataBehavior.MexContractName, _
                  MetadataExchangeBindings.CreateMexHttpBinding(), _
                  "mex")
            'Add application endpoint
            svcHost.AddServiceEndpoint(GetType(HelloWorldService.IService1
                                       ), New WSHttpBinding(), "")
            'Open the service host to accept incoming calls
    
            svcHost.Open()
            Console.WriteLine("service started!")
            Console.Read()
    
    
        End Sub

    You can test the service by input http://localhost/HelloWorldService in the browser when start up the service. You can also create a client to test the service( To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax: svcutil.exe http://localhost/HelloWorldService?wsdl).

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 22, 2013 7:27 AM
    Moderator

All replies

  • WebServiceHost and ServiceEndPoint  can be utilised to configure the service programatically.

    Refer http://msdn.microsoft.com/en-us/library/bb412178.aspx


    Apriori algorithm [association rule]

    Thursday, March 21, 2013 3:19 PM
  • Hi Raju,

    Thanks for your post. I want to override the WCF Service constructor.  I tried the below code but it is not working. Please let me know where I am going wrong. I have removed the configuration settings from App.config file.

    ' NOTE: If you change the class name "HelloWorldService" here, you must also update the reference to "HelloWorldService" in App.config.
    Imports System
    Imports System.ServiceModel
    Imports System.ServiceModel.Description
    
    Public Class HelloWorldService
        Implements IHelloWorldService
        Public Sub New()
            Dim svcHost As New ServiceHost(GetType(HelloWorldService), New Uri("http://localhost/HelloWorldService"))
    
            'Check to see if the service host already has a ServiceMetadataBehavior
            Dim smb As ServiceMetadataBehavior = svcHost.Description.Behaviors.Find(Of ServiceMetadataBehavior)()
            'If not, add one
            If (smb Is Nothing) Then
                smb = New ServiceMetadataBehavior()
            End If
            smb.HttpGetEnabled = True
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15
            svcHost.Description.Behaviors.Add(smb)
            'Add MEX endpoint
            svcHost.AddServiceEndpoint( _
                  ServiceMetadataBehavior.MexContractName, _
                  MetadataExchangeBindings.CreateMexHttpBinding(), _
                  "mex")
            'Add application endpoint
            svcHost.AddServiceEndpoint(GetType(IHelloWorldService), New WSHttpBinding(), "")
            'Open the service host to accept incoming calls
            svcHost.Open()
        End Sub
        
        Public Function SayGreetings() As String Implements IHelloWorldService.SayGreetings
            Return "Hello World"
        End Function
    
    End Class
    

    My Service Interface code is as follows

    Imports System.ServiceModel
    
    ' NOTE: If you change the class name "IHelloWorldService" here, you must also update the reference to "IHelloWorldService" in App.config.
    <ServiceContract()> _
    Public Interface IHelloWorldService
    
        <OperationContract()> _
        Function SayGreetings() As String
    
    
    End Interface

    When I am running the service I am getting the following error.

    Thanks,

    Noor


    Thanks & Regards, Noor Hussain

    Friday, March 22, 2013 2:49 AM
  • Hi,

    It seems that you want to create a service which is self-hosted, you can create a Console application to host the service, and set the host app as StartUp Project. Below is the code that used to host the service.

    Sub Main()
            Dim svcHost As New ServiceHost(GetType(HelloWorldService.Service1
                                           ), New Uri("http://localhost/HelloWorldService"))
    
            'Check to see if the service host already has a ServiceMetadataBehavior
            Dim smb As ServiceMetadataBehavior = svcHost.Description.Behaviors.Find(Of ServiceMetadataBehavior)()
            'If not, add one
            If (smb Is Nothing) Then
                smb = New ServiceMetadataBehavior()
            End If
            smb.HttpGetEnabled = True
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15
            svcHost.Description.Behaviors.Add(smb)
            'Add MEX endpoint
            svcHost.AddServiceEndpoint( _
                  ServiceMetadataBehavior.MexContractName, _
                  MetadataExchangeBindings.CreateMexHttpBinding(), _
                  "mex")
            'Add application endpoint
            svcHost.AddServiceEndpoint(GetType(HelloWorldService.IService1
                                       ), New WSHttpBinding(), "")
            'Open the service host to accept incoming calls
    
            svcHost.Open()
            Console.WriteLine("service started!")
            Console.Read()
    
    
        End Sub

    You can test the service by input http://localhost/HelloWorldService in the browser when start up the service. You can also create a client to test the service( To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax: svcutil.exe http://localhost/HelloWorldService?wsdl).

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 22, 2013 7:27 AM
    Moderator