programmatic WCF service configuration?
-
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! :)
-
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! :)
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
-
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
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
-
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
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: