Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. programmatic WCF service configuration?

programmatic WCF service configuration?

Scheduled Pinned Locked Moved WCF and WF
wcfcsharpwpfxmlhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Don Rolando
    wrote on last edited by
    #1

    Hi everyone, there is quite a large number of samples on programmatic WCF configurations; but none worked for me so far! :( I have the following configuration set up in the app.config file:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <system.serviceModel>
    <behaviors>
    <serviceBehaviors>
    <behavior name="MyTest.MyWebServiceBehavior">
    <serviceMetadata httpGetEnabled="true" />
    <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
    </serviceBehaviors>
    </behaviors>
    <services>
    <service behaviorConfiguration="MyTest.MyWebServiceBehavior"
    name="MyTest.MyWebService">
    <endpoint address="" binding="wsHttpBinding" contract="MyTest.IMyWebService">
    <identity>
    <dns value="localhost" />
    </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
    <baseAddresses>
    <add baseAddress="http://localhost:8088/MyTest/MyWebService/" />
    </baseAddresses>
    </host>
    </service>
    </services>
    </system.serviceModel>
    </configuration>

    I tried several things, e.g. - AddEndpoint; the corresponding port is in use when configuring an endpoint with another port. But no matter which address I use, it's always error 400, forbidden! But the usual service page is not shown, neither is the wsdl description somewhere... - I have overwritten the ServiceHost as described somewhere at the MSDN for those who do not want to use the XML configuration at all. But although behaviour with activated httpgetenabled was set, I have no access to the configured address through the browser. Can anyone give me some lines of code that set exactly the above configuration through the programmatic approach only? Thanks in advance! :)

    M 1 Reply Last reply
    0
    • D Don Rolando

      Hi everyone, there is quite a large number of samples on programmatic WCF configurations; but none worked for me so far! :( I have the following configuration set up in the app.config file:

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
      <system.serviceModel>
      <behaviors>
      <serviceBehaviors>
      <behavior name="MyTest.MyWebServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
      </serviceBehaviors>
      </behaviors>
      <services>
      <service behaviorConfiguration="MyTest.MyWebServiceBehavior"
      name="MyTest.MyWebService">
      <endpoint address="" binding="wsHttpBinding" contract="MyTest.IMyWebService">
      <identity>
      <dns value="localhost" />
      </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      <host>
      <baseAddresses>
      <add baseAddress="http://localhost:8088/MyTest/MyWebService/" />
      </baseAddresses>
      </host>
      </service>
      </services>
      </system.serviceModel>
      </configuration>

      I tried several things, e.g. - AddEndpoint; the corresponding port is in use when configuring an endpoint with another port. But no matter which address I use, it's always error 400, forbidden! But the usual service page is not shown, neither is the wsdl description somewhere... - I have overwritten the ServiceHost as described somewhere at the MSDN for those who do not want to use the XML configuration at all. But although behaviour with activated httpgetenabled was set, I have no access to the configured address through the browser. Can anyone give me some lines of code that set exactly the above configuration through the programmatic approach only? Thanks in advance! :)

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Ignore my last reply - after I posted, I figured it would be more useful to you if I posted a working example instead of an untested one. Good thing, too, since my first code didn't work at all :) Here's a working version, hosted in a console app...

          static void Main(string\[\] args)
          {
              try
              {
                  String baseAddress = "http://localhost:55123/MyTest/MyWebService";
      
                  using (ServiceHost host = new ServiceHost(typeof(MyWebService), new Uri(baseAddress)))
                  {
      
                      ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
                      if (sdb == null)
                      {
                          sdb = new ServiceDebugBehavior();
                          host.Description.Behaviors.Add(sdb);
                      }
                      sdb.IncludeExceptionDetailInFaults = false;
      
                      WSHttpBinding binding = new WSHttpBinding();
                      host.AddServiceEndpoint(typeof(IMyWebService), binding, "");
      
                      ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                      if (smb == null)
                      {
                          smb = new ServiceMetadataBehavior();
                          host.Description.Behaviors.Add(smb);
                      }
                      smb.HttpGetEnabled = true;
      
                      ServiceEndpoint mexendpoint = host.Description.Endpoints.Find(typeof(IMetadataExchange));
                      if (mexendpoint == null)
                      {
                          host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
                      }
      
                      host.Open();
      
                      Console.WriteLine("Service is running.\\n");
                      Console.WriteLine("Press <ENTER> to terminate...");
                      Console.ReadLine();
      
                      host.Close();
                  }
              }
              catch (TimeoutException timeProblem)
              {
                  Console.WriteLine("TimeoutException:");
                  Console.WriteLine(timeProblem.Message);
                  Console.WriteLine(timeProblem.StackTrace);
                  Console.WriteLine("Press <ENTER> to terminate...");
                  Console.ReadLine();
              }
              catch (CommunicationException commProblem)
              {
                  Console.Wri
      
      D 1 Reply Last reply
      0
      • M Mark Salsbery

        Ignore my last reply - after I posted, I figured it would be more useful to you if I posted a working example instead of an untested one. Good thing, too, since my first code didn't work at all :) Here's a working version, hosted in a console app...

            static void Main(string\[\] args)
            {
                try
                {
                    String baseAddress = "http://localhost:55123/MyTest/MyWebService";
        
                    using (ServiceHost host = new ServiceHost(typeof(MyWebService), new Uri(baseAddress)))
                    {
        
                        ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
                        if (sdb == null)
                        {
                            sdb = new ServiceDebugBehavior();
                            host.Description.Behaviors.Add(sdb);
                        }
                        sdb.IncludeExceptionDetailInFaults = false;
        
                        WSHttpBinding binding = new WSHttpBinding();
                        host.AddServiceEndpoint(typeof(IMyWebService), binding, "");
        
                        ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                        if (smb == null)
                        {
                            smb = new ServiceMetadataBehavior();
                            host.Description.Behaviors.Add(smb);
                        }
                        smb.HttpGetEnabled = true;
        
                        ServiceEndpoint mexendpoint = host.Description.Endpoints.Find(typeof(IMetadataExchange));
                        if (mexendpoint == null)
                        {
                            host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
                        }
        
                        host.Open();
        
                        Console.WriteLine("Service is running.\\n");
                        Console.WriteLine("Press <ENTER> to terminate...");
                        Console.ReadLine();
        
                        host.Close();
                    }
                }
                catch (TimeoutException timeProblem)
                {
                    Console.WriteLine("TimeoutException:");
                    Console.WriteLine(timeProblem.Message);
                    Console.WriteLine(timeProblem.StackTrace);
                    Console.WriteLine("Press <ENTER> to terminate...");
                    Console.ReadLine();
                }
                catch (CommunicationException commProblem)
                {
                    Console.Wri
        
        D Offline
        D Offline
        Don Rolando
        wrote on last edited by
        #3

        Thanks a lot, Mark, finally it works! :) Since you said your second post was a tested one, I kept on trying until it was running on my machine. Basically my mistake was to let the app.config consist as "default" configuration. I removed the config file and now your example runs out of the box. Thanks again. :) Best regards, Roland

        M 1 Reply Last reply
        0
        • D Don Rolando

          Thanks a lot, Mark, finally it works! :) Since you said your second post was a tested one, I kept on trying until it was running on my machine. Basically my mistake was to let the app.config consist as "default" configuration. I removed the config file and now your example runs out of the box. Thanks again. :) Best regards, Roland

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Cool :) You can still use an app.config file if you need to - just remove the <system.serviceModel> section. Cheers, Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups