Bind self-hosted WCF service to subdomain - c#

I have 2 instances of a self-hosted wcf service. One dev and one acceptance.
These need to be bound to 'dev.example.com/TestService' and 'acc.example.com/TestService'.
When I start the first service, it binds to 'http://+:80/TestService'.
Requests for both environments are received on the first instance.
And when the second instance is started, it can't bind anymore.
Is there a way to configure the servicehost to only bind on specific subdomain?
Config:
<system.serviceModel>
<services>
<service name="TestProject.TestService" behaviorConfiguration="WebServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://dev.example.com/TestService" />
</baseAddresses>
</host>
<endpoint binding="basicHttpBinding" contract="TestProject.ITestService">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WebServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Code:
_host = new ServiceHost(typeof (TestService));
_host.Open();

Related

migrate to https in a c# self-hosted WFC service

I've got a c# self-hosted WebService with an http endpoint. Everything works great, now I would like to migrate it in HTTPS. I've got the certificate, how can I do? Tnx
This is my app.config:
<system.serviceModel>
<services>
<service name="XWebServiceLib.XWebService" behaviorConfiguration="XWebServiceBehave">
<host>
<baseAddresses>
<add baseAddress="http://10.82.80.21:80/XWebService"/>
</baseAddresses>
</host>
<endpoint address="http://10.82.80.21:80/XWebService" binding="basicHttpBinding" bindingNamespace="http://10.82.80.21:80/XWebService" contract="XWebServiceLib.IXWebService"/>
<endpoint address="mex" binding="mexHttpBinding" bindingNamespace="http://10.82.80.21:80/XWebService" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="XWebServiceBehave">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
And this is how I start my WebService:
var instance = new XWebService();
svcHost = new ServiceHost(instance);
svcHost.Open();
If above configuration works fine, it is enough that changing the binding type to enable Https.
We need to change the security mode of basichttpbinding to transport security mode.
<system.serviceModel>
<services>
<service name="XWebServiceLib.XWebService" behaviorConfiguration="XWebServiceBehave">
<host>
</host>
<endpoint address="https://10.82.80.21:80/XWebService" binding="basicHttpBinding" bindingConfiguration="mybinding" bindingNamespace="http://10.82.80.21:80/XWebService" contract="XWebServiceLib.IXWebService"/>
<endpoint address="mex" binding="mexHttpsBinding" bindingNamespace="http://10.82.80.21:80/XWebService" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="mybinding">
<security mode="Transport">
<transport clientCredentialType="None"></transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="XWebServiceBehave">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Alternatively, we could use the basichttpsbinding without applying the specific binding configuration.
<system.serviceModel>
<services>
<service name="XWebServiceLib.XWebService" behaviorConfiguration="XWebServiceBehave">
<host>
</host>
<endpoint address="https://10.82.80.21:80/XWebService" binding="basicHttpsBinding" bindingNamespace="http://10.82.80.21:80/XWebService" contract="XWebServiceLib.IXWebService"/>
<endpoint address="mex" binding="mexHttpsBinding" bindingNamespace="http://10.82.80.21:80/XWebService" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="XWebServiceBehave">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Subsequently, Https service endpoint requires us to bind a certificate to the specific port.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-a-port-with-an-ssl-certificate
In general, we bind a certificate to the port with the below statement.
netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF}
In IIS, it could be accomplished by the site-binding module.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with-ssl
Feel free to let me know if there is anything I can help with.

Specifying runtime address for WCF service via config

I've created a WCF library using VS2010 C#, made reference to it from other project, everithing works fine on designtime. But on runtime, as I understand it, it uses different address. So, how can I specify it?
My App.Config looks like:
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfService.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/Design_Time_Addresses/WcfService/MyWcf/" />
</baseAddresses>
</host>
<endpoint address ="" binding="wsHttpBinding" contract="WcfService.IService1">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

load wcf hosted on console app with app.config?

Hello im making wcf hosted in console app is possible to load this service with app.config?
ChannelFactory<IService> service = new ChannelFactory<IService>();
service.Endpoint.Behaviors.Add(new WebHttpBehavior());
IService channel = service.CreateChannel();
Console.Read();
This is start of my wcf into console app but how to load wcf config?
Im new in wcf so please be polite for me.
--edit---
So i get errorr
s zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.
ServiceHost serviceHost = null;
serviceHost = new ServiceHost(typeof(Contracs.Service));
serviceHost.Open();
and my app.config is
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="restfulBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Contracs.Service">
<endpoint address="" behaviorConfiguration="restfulBehavior"
binding="webHttpBinding" bindingConfiguration="" contract="Contracs.IService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/Service" />
</baseAddresses>
</host>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
WCF will by default try to load from the app config.
Here is some more info:
http://msdn.microsoft.com/en-us/library/ms733932(v=vs.110).aspx

net.msmq not recognised for a WCF service

Getting the following error when attempting to start a Windows service hosting a WCF service:
Could not find a base address that matches scheme net.msmq for the endpoint with binding NetMsmqBinding. Registered base address schemes are [http].
Works fine if I remove the netmsmq binding and use the basichttp binding. Config is as below:
<system.serviceModel>
<services>
<service name="ManageContactService.ManageContact" behaviorConfiguration="ContactServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8888/ManageContact/ContactService"/>
</baseAddresses>
</host>
<endpoint address="net.msmq//localhost/private/testqueue" binding="netMsmqBinding"
bindingConfiguration="MyMsmqBinding" contract="ManageContactService.IManageContact" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<netMsmqBinding>
<binding name="MyMsmqBinding">
<security mode="None"></security>
</binding>
</netMsmqBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ContactServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
You need a colon:
net.msmq://localhost/private/testqueue

Why does my WCF service give the message 'does not have a Binding with the None MessageVersion'?

I have created a working WCF service. I now want to add some security to it to filter Ip Addresses. I have followed the example that microsoft publish in the samples to try and add a IDispatchMessageInspector that will raise a call AfterReceiveRequest and then throw an error if the ip address is not from the allowed list.
After looking at the code; they have configured it using 'wsHttpBinding', however I want to use 'webHttpBinding' or 'basicHttpBinding'. But when I set it up I get the error:
The endpoint at 'http://upload/api/Api.svc/soap' does not have a
Binding with the None MessageVersion.
'System.ServiceModel.Description.WebHttpBehavior' is only intended for
use with WebHttpBinding or similar bindings.
My configuration is:
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
</serviceHostingEnvironment>
<!--Set up the service-->
<services>
<service behaviorConfiguration="SOAPRESTDemoBehavior" name="HmlApi">
<endpoint address="rest" binding="webHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="SOAPRESTDemoEndpointBehavior" />
<endpoint address="soap" binding="basicHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="SOAPRESTDemoEndpointBehavior" />
</service>
</services>
<!--Define the behaviours-->
<behaviors>
<serviceBehaviors>
<behavior name="SOAPRESTDemoBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<!---Endpoint -->
<endpointBehaviors>
<behavior name="SOAPRESTDemoEndpointBehavior">
<ipFilter/>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="ipFilter" type="VLSCore2.Api.IpFilterBehaviourExtensionElement, VLSCore2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
So what im wondering is how I can set up my message inspector without using WebHttpBinding. Is this even possible?
I want to use SOAP 'basicHttpBinding' not wsHttpBinding (and all of the WS*) associated overheads....
This is simply happening because you have configured a single endpointBehavior for both the SOAP and REST endpoints but the SOAP endpoint can't have the webHttp behavior. You need to split these apart so that they are:
<endpointBehaviors>
<behavior name="SOAPDemoEndpointBehavior">
<ipFilter/>
</behavior>
<behavior name="RESTDemoEndpointBehavior">
<ipFilter/>
<webHttp />
</behavior>
</endpointBehaviors>
and then your endpoints should be:
<endpoint address="rest" binding="webHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="RESTDemoEndpointBehavior" />
<endpoint address="soap" binding="basicHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="SOAPDemoEndpointBehavior" />
For me, this was because I had a 'webHttp' defined as a behaviour for a SOAP config. Only its absence resolved it..
For 4 days, I have been up and down through MS documentation, Stack overflow and everything else. trying to get the configuration file working correctly after I started using HTTPS / SSL for requests from my service. The setup of this configuration file is so critical.
I did not have to modify any of the code switching from HTTP to HTTPS is was all in the web.config file system.serviceModel section.
This config works for using javascript to access JSON data from the ASP.NET c# WCF. I am so happy!
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false">
</serviceHostingEnvironment>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpsGetEnabled="false"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="sfbSecureBinding">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="buildingsWebService.Service1">
<endpoint address="https://www.azsfb.gov/Service/Service1.svc"
binding="webHttpBinding"
bindingConfiguration="sfbSecureBinding"
contract="buildingsWebService.IService1"
behaviorConfiguration="webBehavior">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="https://www.azsfb.gov/"/>
</baseAddresses>
</host>
</service>
</services>
<protocolMapping>
<add binding="basicHttpBinding" scheme="https"/>
</protocolMapping>
</system.serviceModel>

Categories