Accessing and installing app.config based on user specific - c#

I am having 2 app.config files on working for https access and another for http access, if i am having two option button one for http and another for https, if user select 1st option i.e http how i can store inside C:\program files while installing my application and refer my application to use only httpsapp.config file and not httpapp.config file.
Or how i can make my app.config file to work both for https as well as http.
Below is my app.config file while is currently working for http
<system.serviceModel>
<standardEndpoints />
<bindings>
<webHttpBinding>
<binding name="Bind1" crossDomainScriptAccessEnabled="true" maxReceivedMessageSize="900000" maxBufferSize="900000" />
</webHttpBinding>
</bindings>
<services>
<service name="PeripheralServiceImpl" behaviorConfiguration="SvcBhvr">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="EndPBhvr" bindingConfiguration="Bind1" contract="Server.Contract.IPeripheralService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:18732/Peripheral/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="EndPBhvr">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" faultExceptionEnabled="false" defaultBodyStyle="Wrapped" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="SvcBhvr">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
</system.serviceModel>
And app.config working in https
<system.serviceModel>
<standardEndpoints />
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<bindings>
<basicHttpBinding>
<binding name ="soapBinding">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</basicHttpBinding>
<webHttpBinding>
<!--<binding name="httpBinding">-->
<binding name="Bind1" crossDomainScriptAccessEnabled="true" maxReceivedMessageSize="900000" maxBufferSize="900000" >
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="PeripheralServiceImpl" behaviorConfiguration="SvcBhvr">
<host>
<baseAddresses>
<add baseAddress="https://localhost:18732/Peripheral/" />
</baseAddresses>
</host>
<endpoint address="https://localhost:18732/Peripheral/" binding="webHttpBinding" behaviorConfiguration="EndPBhvr" bindingConfiguration="Bind1"
contract="Server.Contract.IPeripheralService">
</endpoint>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="EndPBhvr">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="SvcBhvr">
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Anyone can help me out?

You can use a single config file for http and https, by defining the base address for each one.
I just tried this and it is working fine :
<system.serviceModel>
<services>
<service name="MyTest.MyService" behaviorConfiguration="ServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:80/WCFtest/service.svc" />
<add baseAddress="https://localhost:443/WCFtest/service.svc" />
</baseAddresses>
</host>
<endpoint address="/soap" binding="basicHttpBinding" contract="MyTest.IMathService" />
<endpoint address="/rest" binding="webHttpBinding" contract="MyTest.IMyService" behaviorConfiguration="REST" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

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.

WCF custombinding mutualcertificate keep getting 'The request message must be protected' error

i have a server with wcf soap services. With simple httpbinding configuration everything worked perfectly.
After I changed the web.config to custombinding I keep getting the same error :
The request message must be protected. This is required by an operation of the contract ('IMyServices','http://tempuri.org/'). The protection must be provided by the binding ('BasicHttpBinding','http://tempuri.org/').
This is my web.config :
<system.serviceModel>
<bindings>
<customBinding>
<binding name="MyBinding">
<textMessageEncoding messageVersion="Soap11WSAddressingAugust2004" />
<security authenticationMode="MutualCertificate" requireDerivedKeys="false"
messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" />
<httpTransport keepAliveEnabled="false" />
</binding>
</customBinding>
</bindings>
<services>
<service behaviorConfiguration="MyBehavior" name="NewWCF.MyServices">
<endpoint address="" binding="customBinding" bindingConfiguration="MyBinding"
name="wsHttpMembershipEndpoint" contract="NewWCF.IMyServices" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://10.130.111.111/NewWCF" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceCredentials>
<clientCertificate>
<certificate findValue="CN=Central-Gateway" x509FindType="FindBySubjectDistinguishedName" storeName="TrustedPeople" />
<authentication revocationMode="NoCheck"
certificateValidationMode = "None"/>
</clientCertificate>
<serviceCertificate findValue="CN=MySrvTst" x509FindType="FindBySubjectDistinguishedName" storeName="My" storeLocation="LocalMachine" />
</serviceCredentials>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceSecurityAudit serviceAuthorizationAuditLevel="SuccessOrFailure"
messageAuthenticationAuditLevel="SuccessOrFailure" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
lang-xml -->
Both certificates appear correctly in mmc.exe
I try to test it through a browser calling .svc path , should it work that way or does it fail because of the certificates ?
Is there a way to see a more detailed error?
What can be the problem?
Thanks in advance,
Brian.

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

WCF Service End Point Not Found

i have deployed my wcf service on IIS.. Its giving me this error when I want to access its one of method "EndPoint Not Found" While locally its working fine for me and returning me data.
Here is my Web.config Bindings Information
<system.webServer>
<directoryBrowse enabled="false" />
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="RTAService" behaviorConfiguration="WtfServiceBehaviour">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="wtfSslBinding" behaviorConfiguration="WebHttpBehaviour" contract="IRTAService" />
<endpoint address="soap" binding="basicHttpBinding" contract="IRTAService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="wtfSslBinding" />
<binding name="streamedBinding"
maxBufferSize="65536"
maxReceivedMessageSize="2000000000"
transferMode="Streamed">
<readerQuotas maxDepth="500000000"
maxArrayLength="500000000" maxBytesPerRead="500000000"
maxNameTableCharCount="500000000" maxStringContentLength="500000000"/>
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="WebHttpBehaviour">
<webHttp helpEnabled="true" defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json"
automaticFormatSelectionEnabled="false" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WtfServiceBehaviour">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Please tell me any work around for that.
I dont see endpoint defined any where . Your config should have something like this.`
<endpoint address="http://localhost:57800/Services.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IServices" contract="MYServices.IServices"
name="BasicHttpBinding_IServices" />

wcf config tcp and http

hi im working on wcf that has http + tcp services
thats my config:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
<baseAddressPrefixFilters>
<add prefix="http://localhost/API/"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
<services>
<service name="API.APIsrv" behaviorConfiguration="ServiceBehaviour">
<endpoint address="http://localhost/tAPI/Basic" binding="basicHttpBinding" name="Basic" bindingConfiguration="Basic" contract="API.IAPIsrv" >
</endpoint>
<endpoint address="http://localhost/API" binding="webHttpBinding" name="APIsrv" contract="API.IAPIsrv" behaviorConfiguration="web">
</endpoint>
<endpoint address="net.tcp://localhost/API" binding="netTcpBinding" name="APIsrv.netTcpBinding" contract="API.IAPIsrv">
</endpoint>
<endpoint address="http://localhost/API/mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<endpoint address="net.tcp://localhost/API/mex"
binding="mexTcpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost/API/APIsrv.svc"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="Basic" />
</basicHttpBinding>
<netTcpBinding>
<binding name="APIsrv.netTcpBinding" transferMode="Streamed" maxReceivedMessageSize="20480000">
<security mode="None"></security>
</binding>
</netTcpBinding>
</bindings>
in my global file i handle route address:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("", new ServiceHostFactory(), typeof(APIsrv)));
}
in my local client test i get this error:
The service '/API/' cannot be activated due to an exception during compilation. The exception message is: The ChannelDispatcher at 'http://localhost/API/Basic' with contract(s) '"IAPIsrv"' is unable to open its IChannelListener.
how can i fix this issue

Categories