How To Change The Binding of a WCF Service - wcf

I have a very simple (see any getting started sample online) wcf service library. And i can run it via the WCF Test Client exe on localhost. But when accessing it via another client tool, like soapUI, i get errors. I believe the errors are related to security configuration of the service.
How can i change the binding of a service, like is suggested in this post:
"Invalid or expired security context token" when running after a debugging restart
Is this in app.config or web.config. my project doesnt even have a web.config.
Here's my app.config:
<?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 name="NorthwindServices.ProductService">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/Design_Time_Addresses/NorthwindServices/ProductService/" />
</baseAddresses>
</host>
<endpoint address ="" binding="wsHttpBinding"
contract="NorthwindServices.IProducts">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- 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="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

Related

WCF service metadata (wsdl) cannot be accessed: error 400

I have a problem with WCF metadata on a new installation ( IIS 10 on Windows Server 2016)
Situation
WCF service running fine (as it used to do on previous installation)
Navigation (via browser) of wsdl is NO more possible, or better
http://ServiceA/ServiceA.svc: yes (web page available) but link to xml not working (error 400 from IIS)
https://ServiceA/ServiceA.svc: not even the page
Moreover in the serviceMetadata it is stated to use https no http, that is in contrast with the behaviour just described.
Below an extract of web.config
<services>
<service behaviorConfiguration="ServiceBehavior" name="...">
<endpoint address="https://<fqdn>/ServiceA/ServiceA.svc"
binding="wsHttpBinding"
bindingConfiguration="wsHttpEndpointBinding"
name="WsHttpBinding"
contract="ServiceContracts.IServiceA">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
...
Could it be something linked to name resolution?
The PC name is AAAAAA, the FQDN is BBBBB.domain.ext
Thanks for any suggestion
L.
The WCF service deployed in IIS does not need to set the endpoint address in web.config.
Here is my demo:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true">
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows" />
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
<system.webServer>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
<system.serviceModel>
<services>
<service name="WcfService25.Service1" behaviorConfiguration="WcfService25.Service1Behavior">
<!-- Service Endpoints -->
<endpoint address="" binding="basicHttpBinding" contract="WcfService25.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService25.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 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="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
This is web.config. This project is a template project.
The base address of the WCF service in IIS is as follows:

WCF Json Service not configured correctly

Using .Net 3.5. Trying to provide a WCF - Json service.
Looked for implementation examples and after failing with a few went with Code Project "How to create a JSON WCF RESTful Service in 60 seconds"
This is my App.config
<?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 name="WcfJsonRestService.Service1" behaviorConfiguration="WcfJsonRestService.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/Design_Time_Addresses/WcfJsonRestService/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="http://localhost:8732/service1" binding="webHttpBinding" contract="WcfJsonRestService.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WcfJsonRestService.Service1Behavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WcfJsonRestService.Service1Behavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- 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="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
In my browser I run:
`http://localhost:8732/Service1/data/10`
And I get
<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none">
<Code>
<Value>Sender</Value>
<Subcode>
<Value xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:DestinationUnreachable</Value>
</Subcode>
</Code>
<Reason>
<Text xml:lang="en-GB">
The message with To 'http://localhost:8732/Service1/data/10' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.
</Text>
</Reason>
</Fault>
Help would be appreciated
try to add this
[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]

WCF basicHttpsBinding warning: "is invalid according to its datatype"

I created a WCF application, starting with basicHttpBinding. In app.config, I have the following endpoint configured:
<endpoint address="" binding="basicHttpBinding" contract="JMMEcommerceService.IJMMEcommerceService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
This builds fine. However, I want to change the protocol from HTTP to HTTPS. If I change the endpoint declaration to:
<endpoint address="" binding="basicHttpsBinding" contract="JMMEcommerceService.IJMMEcommerceService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
(note: the only change is basicHttpBinding -> basicHttpsBinding)
I get the following warning a build time:
WCF configuration validation warning: The 'binding' attribute is invalid - The value 'basicHttpsBinding' is invalid according to its datatype 'serviceBindingType'.
Why would I get this warning? Is there something else that I need to change?
Here is the full app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<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 name="JMMEcommerceService.JMMEcommerceService">
<endpoint address="" binding="basicHttpsBinding" contract="JMMEcommerceService.IJMMEcommerceService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="https://localhost:8733/Design_Time_Addresses/JMMEcommerceService/JMMEcommerceService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- 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>
</system.serviceModel>
</configuration>
The basicHttpsBinding does not appear to exist in .NET 4.0, only in 4.5. Which version of .NET are you targeting?
If you like to enable HTTPS for your service, but not for your Metadata, you have to provide another base address for your HTTP-based Metadata Exchange endpoint.
<baseAddresses>
<add baseAddress="https://localhost:8733/Design_Time_Addresses/JMMEcommerceService/JMMEcommerceService/" />
<add baseAddress="http://localhost:8734/Design_Time_Addresses/JMMEcommerceService/JMMEcommerceService/" />
</baseAddresses>
Please note, that you have to assign another port too (8734). Two protocols can not use the same port!
PS: Do not forget to assign the correct certificate to the port 8733

What is wrong with my WCF Meta Data Configuration?

This is a follow on from this question I've configured the a WCF service running in IIS (From within Visual Studio 2010) to run one Web service and one net.tcp service. After a lot of hacking I've managed to get the Web Service to load into the WCF test client, but I can't get the Net.Tcp service to load into the test client.
Here is my Web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="ServerService" behaviorConfiguration="ServerServiceBehaviour">
<endpoint address="ServerService.svc"
binding="netTcpBinding"
bindingConfiguration="DefaultNetTcpBindingConfig"
name="NetTcpEndPoint"
contract="IServerService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange"
bindingConfiguration="mexTcpBinding"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/"/>
</baseAddresses>
</host>
</service>
<service name="MyWebService" behaviorConfiguration="WebServiceBehaviour">
<endpoint address="MyWebService.svc"
binding="wsHttpBinding"
bindingConfiguration="DefaultWSBinding"
name="MyWSEndPoint"
contract="IMyWebService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
bindingConfiguration="mexHttpBinding"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8523/"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="DefaultNetTcpBindingConfig"
maxConnections="5"
portSharingEnabled="true" >
</binding>
<!--<binding name="mexBinding"
portSharingEnabled="true">
<security mode="None"></security>
</binding>-->
</netTcpBinding>
<wsHttpBinding>
<binding name="DefaultWSBinding"/>
</wsHttpBinding>
<mexTcpBinding>
<binding name="mexTcpBinding"/>
</mexTcpBinding>
<mexHttpBinding>
<binding name="mexHttpBinding"/>
</mexHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServerServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- 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="false"/>
</behavior>
<behavior name="MexBehaviour">
<serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>
</behavior>
<behavior name="WebServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- 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="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
When I run the Visual Studio Debugger to publish the Service I can access the web service by entering the following url into the WCF Test Application:
http://localhost:8523/MyWebService.svc
But if I enter the following Url into the WCF Test Application I get an error:
net.tcp://localhost:8523/ServerService.svc
Here is the Error I see:
Error: Cannot obtain Metadata from
net.tcp://localhost:8523/ServerService.svc If this is a Windows (R)
Communication Foundation service to which you have access, please
check that you have enabled metadata publishing at the specified
address. For help enabling metadata publishing, please refer to the
MSDN documentation at
http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange
Error URI: net.tcp://localhost:8523/ServerService.svc Metadata
contains a reference that cannot be resolved:
'net.tcp://localhost:8523/ServerService.svc'. You have tried to
create a channel to a service that does not support .Net Framing. It
is possible that you are encountering an HTTP endpoint. Expected
record type 'PreambleAck', found '72'.
Having read this question is it possible that the webserver in VS 2010 doesn't support net.tcp binding?
It looks like the issue I was seeing was that the Web Server in Visual Studio 2010 doesn't support Net.Tcp Binding.
I've got slightly further by installing IIS7 (IIS6 doesn't support Net.Tcp either) and telling visual studio to use IIS instead.
You can do this by going to the properties page of your service and selecting the "Web Tab" select the Use Local IIS Web Server radio Button and configure the webserver.
You can also tell is to start an external program on this tab to make it start the WcfTestClient.exe and pass in the URL to your services as command line parameters.
I'm still having issues but they are different issues so I'll open another question.

WCF web service not working after deployed

I am trying to create a WCF web service which will allow other applications to retrieve a string by making a http request to this service url. I tried publishing the service in IIS and when attempting to browse to it, using the url, it says it
' The resource cannot be found'
when I checked the path to the folder I used the url,
and I get the error
'No protocol binding matches the given address
'http://localhost:xxxx/WcfSampleLibrary/Service1/mex.'
Protocol bindings are configured at the Site level in IIS or WAS configuration'
Here is the directory path of the published folder:
C:\inetpub\wwwroot\WcfServices\WcfSampleLibrary\WcfSampleLibrary.Service1
C:\inetpub\wwwroot\WcfServices\WcfSampleLibrary\Web.config
C:\inetpub\wwwroot\WcfServices\WcfSampleLibrary\bin\WcfSampleLibrary.dll
The web config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfSampleLibrary.Service1" behaviorConfiguration ="mex">
<host>
<baseAddresses>
<add baseAddress = "http://192.xxx.x.xxx/WcfSampleLibrary/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<endpoint address =""
binding="wsHttpBinding" contract="WcfSampleLibrary.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<endpoint address="http://localhost:xxxx/WcfSampleLibrary/Service1/mex" name="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mex">
<serviceMetadata httpGetEnabled="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>
In IIS-hosted WCF services you don't specify a full URI in the address. IIS decides the address. Also the baseAddresses element is completely ignored when hosting in IIS (so remove it from you Web.config). The service's base address is determined by the web site & virtual directory into which your wcf service is placed.Do something like this:
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
Then your address would be http://IIS.SERVER/SiteName/Folder/WcfSampleLibrary.Service1.svc. If you're not sure what the address is, use your IIS Administration tool, select the site that has the service in it, Right-click and choose Advanced -> Browse Site.
Also, I'd turn on httpGetEnabled on your mex behavior--if you want to publish your WSDL. This makes it easier to consume your service as you are developing it:
<behaviors>
<serviceBehaviors>
<behavior name="mex" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
With httpGetEnabled being on, browsing to your service URI will give you an option to see the WSDL.