I have created a wcf service, and want to run on secure way(https)
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>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary1.Service1" behaviorConfiguration ="MyServiceTypeBehaviors">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8734/Service1/" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
I have tried to change the base address from http to https.
And I have created self sign certificate from IIS and bind it with https with port 8374
Try with that configuration. I have changed the binding from BasicHttpBinding to BasicHttpsBinding and added "serviceCredentials" section where you have to select your certificate from some store on your machine.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary1.Service1" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="basicHttpsBinding" contract="WcfServiceLibrary1.IService1"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceCredentials>
<serviceCertificate findValue="" storeLocation="LocalMachine" x509FindType="FindByThumbprint" storeName="My"/>
</serviceCredentials>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
</configuration>
P.S. If the web service is hosted in IIS then you don't need baseAddress and port. Port Binding is done from the IIS Manager
Related
I'm trying to call a WCF service from another WCF service, but when I try to call a specific method I get "System.ServiceModel.ProtocolException: 'The remote server returned an unexpected response: (400) Bad Request.'" . Both services are hosted on IIS. This is the configuration of the service I'm calling:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" maxRequestLength="2000000"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service behaviorConfiguration="serviceBehaviour" name="AuthenticationService.WcfService">
<endpoint address="rest" name="AuthEndpoint"
behaviorConfiguration="webBehaviour" binding="webHttpBinding" bindingConfiguration="webBindingConfig"
contract="Interfaces.Wcf.IWcfAuthenticationService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/AuthenticationService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="serviceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webBindingConfig" maxBufferPoolSize="2000000" maxReceivedMessageSize="2000000" >
<readerQuotas maxDepth="2000000" maxStringContentLength="2000000" maxArrayLength="2000000"
maxBytesPerRead="2000000" maxNameTableCharCount="2000000" />
</binding>
</webHttpBinding>
</bindings>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" />
</protocolMapping>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
In the cilent configuration I have:
<client>
<endpoint address="http://localhost/AuthenticationService/WcfService.svc/rest" name="AuthEndpoint"
behaviorConfiguration="webBehaviour"
binding="webHttpBinding"
bindingConfiguration="webBindingConfig"
contract="Interfaces.Wcf.IWcfAuthenticationService" />
</client>
I'm creating the service instance with the ChannelFactory class like this:
var channelFactory = new ChannelFactory<IWcfAuthenticationService>("AuthEndpoint");
var service = channelFactory.CreateChannel();
I am trying to host a service using UDP in WCF but I can't generate a proxy from the service. I need to host the service on a LAN. Can anyone give an example for a server and client using UDP in WCF?
Here is my app.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<extensions>
<bindingElementExtensions>
<add name="udp_Transport" type="Microsoft.ServiceModel.Samples.UdpTransportElement, UdpTransport" />
</bindingElementExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<udpBinding>
<binding name="UDPBinding" openTimeout="24.20:31:23.6470000" receiveTimeout="24.20:31:23.6470000" sendTimeout="24.20:31:23.6470000" maxBufferPoolSize="10000000" maxReceivedMessageSize="10000000">
<readerQuotas maxDepth="10000000" maxStringContentLength="10000000" maxArrayLength="10000000" maxBytesPerRead="10000000" maxNameTableCharCount="10000000"/>
</binding>
</udpBinding>
</bindings>
<services>
<service name="UDP_Server.Service1">
<endpoint address="soap.udp://localhost:40000/Service1/" binding="udpBinding" bindingConfiguration="UDPBinding" contract="UDP_Server.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="soap.udp://localhost:40000/Service1/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
Check the link below:
https://www.codeproject.com/Articles/757349/Multicasting-using-UdpBinding-in-WCF
It also have a code example.
This is my Web.config in WCF service:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="Mobile55.Service1">
<endpoint address="../Service1.svc"
binding="webHttpBinding"
contract="Mobile55.IService1"
behaviorConfiguration="webBehaviour" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
This is the first time I am working with WCF and I cannot add an endpoint for this service in my WinForms application.
I added this service as a reference and I can access the methods, but when I Debug and try to call a method it throws an exception:
Could not find default endpoint element that references contract 'MyService.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
My application app.config file is empty.
I tried the following answers, but didn't work for me:
Could not find default endpoint element
Could not find default endpoint element that references contract - Hosting wcf
WCF Error - Could not find default endpoint element that references contract 'UserService.UserService'
How do I add an endpoint to finally be able to use my service?
Thanks in advance.
EDIT: My app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Mobile55.Service1">
<endpoint address=""
binding="webHttpBinding"
contract="Mobile55.IService1"
behaviorConfiguration="webBehaviour" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="webBehaviour">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost:1008/Service1.svc"
binding="webHttpBinding"
contract="Mobile55.IService1"
behaviorConfiguration="webBehaviour" />
</client>
</system.serviceModel>
</configuration>
Well, the error message specifically says in the client configuration section.
So the error occurs client-side, i.e. from the code where you try to invoke on your service - more is hard to tell without a proper exception/stacktrace and a complete/compilable example that exhibits the behaviour.
Anyway, your configuration file is missing the client section completely.
Add something like this:
<system.serviceModel>
<!-- ... other stuff you have ... -->
<client>
<endpoint address="../Service1.svc"
binding="webHttpBinding"
contract="Mobile55.IService1"
behaviorConfiguration="webBehaviour" />
</client>
</system.serviceModel>
Something like this should do the trick hopefully
<service name="MyService.Service1">
<endpoint address="YourEndPointHere"
binding="webHttpBinding"
contract="MyService.IService1"
behaviorConfiguration="webBehaviour" />
</service>
You can try with this
<service name="Mobile55.Service1">
<endpoint address=""
binding="webHttpBinding"
contract="Mobile55.IService1"
behaviorConfiguration="webBehaviour" />
</service>
You can call your rest service with like this
localhost:port/Service1.svc/Pathname
Here Pathname is which you mentioned in operationcontract of uritemplate.
Hope it helps!
<system.serviceModel>
<client>
<endpoint address="http://localhost:1008/Service1.svc"
binding="webHttpBinding"
bindingConfiguration="webHttpBinding_IService"
contract="Mobile55.IService1"
name="webHttpBinding_IService" />
</client>
</system.serviceModel>
----------
## Heading ##
Try....
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>
I have a WCF service that uses NetTcpBinding and I'd like to host it in a WPF application. The service seems to start correctly, but when I'm trying to get it's metadata using 'Add service reference' in visual studio I get this exception:
The URI prefix is not recognized.
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8000/Mandrake/mex'.
My service project's App.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="Mandrake.Service.OTAwareService">
<endpoint address="OTService" binding="netTcpBinding" contract="Mandrake.Service.IOTAwareService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint name="MEX" address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8000/Mandrake/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
And the code in the hosting application:
Uri baseAddress = new Uri("net.tcp://localhost:8000/Mandrake");
ServiceHost host = new ServiceHost(typeof(OTAwareService), baseAddress);
try
{
host.AddServiceEndpoint(typeof(IOTAwareService), new NetTcpBinding(), "OTService");
}
catch (CommunicationException e)
{
Console.WriteLine(e.Message);
host.Abort();
}
The solutions I found to the problem were mainly about adding the 'serviceMetaData' to the service config or providing a mex endpoint. Could you suggest something?
Edit:
Final config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior0">
<serviceMetadata />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Mandrake.Service.OTAwareService" behaviorConfiguration="NewBehavior0">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8036/OTService"/>
</baseAddresses>
</host>
<endpoint address="" binding="netTcpBinding" name="TcpEndpoint" contract="Mandrake.Service.IOTAwareService" />
<endpoint address="mex" binding="mexTcpBinding" name="MetadataEndpoint" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
Hosting application:
host = new ServiceHost(typeof(OTAwareService));
host.Open();
I've managed to figure it out, after enabling the serviceDebug's includeExceptionDetailInFaults it was pretty clear.
Mandrake.Service.IOTCallback.Send operation references a message element [http://tempuri.org/:Send] that has already been exported from the Mandrake.Service.IOTAwareService.Send operation
So there was a Send(OTMessage) operation in the service contract and in the callback interface as well. A rather ugly mistake but I thought I would leave the solution here in case it helps anyone.