How to access wcf service from another computer? - asp.net

I have a wcf service and i install it as windows service.
I can access this service from 192.168.2.6 machine:
"net.tcp://192.168.2.5:2025/Services/mex".
i want to access this service from another computer using static ip and port.
How can access this service ?
I tried to connect net.tcp://staticIp:port/Services/mex and i got error :
Metadata contains a reference that cannot be resolved: 'net.tcp://[staticIP]:[port]/Services/mex'.If the service is defined in the current solution, try building the solution and adding the service reference again.
(I navigate my [port] to inside port 2025)
my config:
<system.serviceModel>
<diagnostics>
<messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
</diagnostics>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IServices" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://192.168.2.5:2025/Services" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IServices" contract="myServices.IServices"
name="NetTcpBinding_IServices">
<!--<identity>
<dns value="localhost" />
</identity>-->
</endpoint>
</client>
<services>
<service name="F8ShadowWcfLib.Services">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="F8ShadowWcfLib.IServices">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://192.168.2.5:2025/Services" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
Edit1:
I remove tag from config and i add it at runtime.
myService.ServicesClient myServ = new myService.ServicesClient();
EndpointAddress myEndpointAdd = new EndpointAddress(new Uri("net.tcp://[staticIP]:[port]/Services") ,
EndpointIdentity.CreateDnsIdentity("net.tcp://f8srv.f8.com.tr:2299/Services"));
myServ.Endpoint.Address = myEndpointAdd;
I got different error : The server has rejected the client credentials.

The problem is probably related to this part:
<identity>
<dns value="localhost" />
</identity>
This work work locally because localhost makes sense locally, but across a network it doesn't.
You can validate this identity in a number of ways, such as specifying the UPN (user principal name) of the user running the service, or an SPN (Server Principal Name) of the server running the service (although for this you'll have to register a SPN).
This article should explain it a little:
http://msdn.microsoft.com/en-us/library/ms733130.aspx

To allow seperate connection set AddressFilterMode : Any
and set your identy both service and client side.
This article about identy settings:
http://msdn.microsoft.com/en-us/library/ms733130.aspx
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Services : IServices
{
.
.
.
}

Related

the resource cannot be found wcf service with SSL

I created a REST api using asp.net vb and I was trying to invoke the api through secure connection (https) but I had an error
The resource cannot be found
I can invoke any method using (http), but with (https) I can't. And I can access the main page of api (service.svc) using the (https) but the problem with functions!! below are my config and function header.
<system.serviceModel>
<services>
<service name="RESTAPI" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="customBinding" binding="customBinding" bindingConfiguration="basicConfig" contract="RESTAPI"/>
<endpoint address="" behaviorConfiguration="HerbalAPIAspNetAjaxBehavior"
binding="webHttpBinding" contract="HerbalAPI" />
<endpoint contract="RESTAPI" binding="mexHttpBinding" address="mex" />
</service>
</services>
<!-- **** Services ****-->
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="HerbalAPIAspNetAjaxBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="basicConfig">
<binaryMessageEncoding/>
<httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/>
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
API Class
<ServiceContract(Namespace:="")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class RESTAPI
<OperationContract()>
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)>
Public Function test(ByVal st As String) As JSONResultString
//any code
End Function
End Class
You need to define a special binding configuration in your web.config file to allow the SVC service to bind correctly for HTTPS requests.
Please have a look at this blog post: https://weblogs.asp.net/srkirkland/wcf-bindings-needed-for-https
Your service will already be defined in the web.config, just add the bindingConfiguration attribute:
<services>
<service name="TestService">
<endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="webBindingHttps" contract="TestService" />
</service>
</services>
Then define the special binding settings for the webHttpBinding as so, the magic part that fixes the HTTPS request is the <security mode="Transport" />:
<bindings>
<webHttpBinding>
<binding name="webBindingHttps">
<security mode="Transport">
</security>
</binding>
</webHttpBinding>
</bindings>
This will effectively switch the service over to HTTPS, but if you want to have both HTTP and HTTPS to work you need to define 2 binding configurations and then have 2 identical endpoints per service, where the one uses the http bindingConfiguration and the other uses the https bindingConfiguration like so:
<bindings>
<webHttpBinding>
<binding name="webBindingHttps">
<security mode="Transport">
</security>
</binding>
<binding name="webBindingHttp">
<!-- Nothing special here -->
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="TestService">
<endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="webBindingHttps" contract="TestService" />
<endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="webBindingHttp" contract="TestService" />
</service>
</services>

How can I host a WCF service without an SVC file in IIS

I'd like to deploy a dual interface (SOAP/REST/XML/JSON) WCF service in IIS with just a config file and the binaries and no svc file in the URL
We use VS2012 and .Net 4.5
We have something like it working, I followed a guide here:
http://blogs.msdn.com/b/rjacobs/archive/2010/04/05/using-system-web-routing-with-data-services-odata.aspx
I added a Global class with
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
DataServiceHostFactory factory = new DataServiceHostFactory();
RouteTable.Routes.Add(new ServiceRoute("wrap", factory, typeof(NeOtheWrapper)));
}
}
And I used my existing web.config which defines all the endpoints:
<system.serviceModel>
<!-- Clients -->
<client>
<endpoint name="MySoftLive" address="https://backof.somewebsitett.com/Cmp.MySoft.bl/MySoft.svc" binding="basicHttpsBinding" bindingConfiguration="soapSecureBindingConfig" contract="Cmp.MySoft.BL.WCF.MySoftInterface" />
<endpoint name="MySoftTest" address="http://localhost:49957/MySoft.svc" binding="basicHttpBinding" bindingConfiguration="soapBindingConfig" contract="Cmp.MySoft.BL.WCF.MySoftInterface" />
</client>
<!-- Services -->
<services>
<service name="Cmp.MySoft.BL.NeOthe.NeOtheWrapper">
<endpoint name="rest" address="rest" behaviorConfiguration="restEndpointBehaviour" binding="webHttpBinding" bindingConfiguration="restBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
<endpoint name="restSecure" address="rest" behaviorConfiguration="restEndpointBehaviour" binding="webHttpBinding" bindingConfiguration="restSecureBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
<endpoint name="mex" address="mex" behaviorConfiguration="" binding="mexHttpBinding" bindingConfiguration="mexBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
<endpoint name="mexSecure" address="mex" behaviorConfiguration="" binding="mexHttpsBinding" bindingConfiguration="mexSecureBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
<endpoint name="soap" address="soap" behaviorConfiguration="" binding="basicHttpBinding" bindingConfiguration="soapBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
<endpoint name="soapSecure" address="soap" behaviorConfiguration="" binding="basicHttpsBinding" bindingConfiguration="soapSecureBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
</service>
</services>
<!-- Binding Configurations -->
<bindings>
<webHttpBinding>
<binding name="restBindingConfig">
<security mode="None"/>
</binding>
<binding name="restSecureBindingConfig">
<security mode="Transport"/>
</binding>
</webHttpBinding>
<mexHttpBinding>
<binding name="mexBindingConfig"/>
</mexHttpBinding>
<mexHttpsBinding>
<binding name="mexSecureBindingConfig"/>
</mexHttpsBinding>
<basicHttpsBinding>
<binding name="soapSecureBindingConfig">
<security mode="Transport"/>
</binding>
</basicHttpsBinding>
<basicHttpBinding>
<binding name="soapBindingConfig">
<security mode="None"/>
</binding>
</basicHttpBinding>
</bindings>
<!-- Behaviour Configurations -->
<behaviors>
<endpointBehaviors>
<behavior name="restEndpointBehaviour">
<webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" faultExceptionEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
<!-- Hosting Environment Settings -->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
It compiles, runs and if I browse to http://mypc:12345/wrap/rest/help I get the auto generated ASP.NET REST help page.
But, if I go to http://mypc:12345/wrap/soap/ I get 400 Bad Request.
I can't suffix that with ?wsdl to get the wsdl, or pass the url to svcutil (soap+xml not expected)
I was hoping the .SVC SOAP place holder page would appear, same as /help does for REST.
If I browse to the .svc file (it's at the same level as /wrap) that works and the soap service works, as does meta data publishing.
Am I using the wrong URL or is my configuration wrong?
If you're using WCF 4.0 or later, you can use "file-less" activation. Add something like the following to config your file:
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory"
relativeAddress="Soap.svc"
service="Cmp.MySoft.BL.NeOthe.NeOtheWrapper" />
</serviceActivations>
</serviceHostingEnvironment>
This allows you to host a WCF service without a physical .svc file. The relativeAddress is relative to the base address of the site, and you'll need to create the IIS application as usual.
See the "File-Less activation" section in A Developer's Introduction to Windows Communication Foundation 4 for more information.

Endpoint of JSONP webservice not found even though registered in web.config

I have just added a new webservice, but when I approach it, I get: Endpoint not found.
on URL http://www.testsite.com/service.svc/getcompanyreviewdetails/?id=315&t=1
I have another service where the endpoint IS found:
http://www.testsite.com/service.svc/getshopitems/?newurl=myurl
When I request http://www.testsite.com/service.svc?wsdl, I also don't see the getcompanyreviewdetails method in that list.
Here's my code:
Iservice.vb
Namespace RestService
<ServiceContract()>
Public Interface Icompanyservice
<OperationContract()> _
<Web.WebInvoke(Method:="GET", ResponseFormat:=Web.WebMessageFormat.Json, BodyStyle:=Web.WebMessageBodyStyle.Bare, _
UriTemplate:="getcompanyreviewdetails/?id={id}&t={t}")> _
Function getCompanyReviewDetails(ByVal id As Integer, ByVal t As Integer) As Stream
End Interface
<ServiceContract()>
Public Interface Iservice
<OperationContract()> _
<Web.WebInvoke(Method:="GET", ResponseFormat:=Web.WebMessageFormat.Json, BodyStyle:=Web.WebMessageBodyStyle.Bare, _
UriTemplate:="getshopitems/?newurl={newurl}")> _
Function getShopItems(ByVal newURL As String) As Stream
End Interface
End Namespace
service.svc.vb
Namespace RestService
<ServiceContract(Namespace:="RestService")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class companyservice
Implements Icompanyservice
Public Function getCompanyReviewDetails(ByVal id As Integer, ByVal t As Integer) As Stream Implements Icompanyservice.getCompanyReviewDetails
End Function
End Class
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class service
Implements Iservice
Public Function getShopItems(ByVal newURL As String) As Stream Implements Iservice.getShopItems
End Function
End Class
End Namespace
when adding a service reference I don't see the companyservice
solution explorer
web.config
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<client/>
<services>
<service name="RestService.service">
<endpoint behaviorConfiguration="webHttp" binding="webHttpBinding" contract="RestService.Iservice" />
</service>
<service name="RestService.companyservice">
<endpoint address="http://www.testsite.com/service.svc" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="RestService.Icompanyservice" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp/>
</behavior>
<behavior name="webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
UPDATE: configuring 2 endpoints
<client>
<endpoint name="basic" address="http://www.testsite.com/service.svc" binding="basicHttpBinding" contract="RestService.Iservice" />
</client>
<services>
<service name="RestService.service" behaviorConfiguration="webHttpBehavior"> <!-- in this case the property behaviorConfiguration is incorrect according to its datattype serviceBehaviorConfigurationType, I don't know what other value to provide here -->
<endpoint address="" binding="basicHttpBinding" contract="RestService.Iservice" />
</service>
</services>
I then tried:
<client />
<services>
<service name="RestService.service">
<endpoint behaviorConfiguration="webHttp" binding="webHttpBinding" contract="RestService.Iservice" />
<endpoint address="http://www.testsite.com/service.svc" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="RestService.Icompanyservice" />
</service>
</services>
The contract name 'RestService.Icompanyservice' could not be found in the list of contracts implemented by the service 'service'.
So I tried moving all method of Icompanyservice under Iservice again and remove everything related to Icompanyservice. Also changed the web.config to:
<service name="RestService.service">
<endpoint behaviorConfiguration="webHttp" binding="webHttpBinding" contract="RestService.Iservice" />
<endpoint address="http://www.testsite.com/service.svc" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="RestService.Iservice" />
</service>
I then got:
When 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' is set to true in configuration, the endpoints are required to specify a relative address. If you are specifying a relative listen URI on the endpoint, then the address can be absolute. To fix this problem, specify a relative uri for endpoint 'http://www.testsite.com/service.svc'.
So I set it to:
<endpoint address="/service.svc" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="RestService.Iservice" />
Then both services work, but how can I validate if service getcompanyreviewdetails is now JSONP instead of JSON?
Example.svc.vb no interface.
<ServiceContract(Namespace:="{namespace the service resides in}")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class ExampleService
Then adorn your methods in here as you would <OperationalContract>MethodName...
ServiceReference.ClientConfig - I have 2 endpoints one for testing local and the other for publish - remember to comment out one or the other depending on what your doing(publishing vs testing).
<endpoint address="http://{your domain}/ExampleService.svc"
binding="customBinding" bindingConfiguration="CustomBinding_ExampleService"
contract="ExampleServRef.ExampleService" name="CustomBinding_ExampleService" />
<endpoint address="http://{localhost:port}/ExampleService.svc"
binding="customBinding" bindingConfiguration="CustomBinding_ExampleService"
contract="ExampleServRef.ExampleService" name="CustomBinding_ExampleService" />
A Developer's Introduction to Windows Communication Foundation
WCF Multiple Endpoints

Is it possible to have more than one Wcf service class ? If yes then how?

Is it possible to have more than one Wcf service class . Because I have two contract interface and want to implement in two different service classes. Is it possible?
If you have 2 contracts say IService1 and IService2, you have two options :
both 2 service contracts are implemented by one service which implement all méthod from IService1 and IServe2. Then your configuration file will look like :
<services>
<service name="MyNamespace.Service">
<host>
<baseAddresses>
<add baseAddress="http:localhost:8080" />
</baseAddresses>
</host>
<endpoint address="Service1" binding="basicHttpBinding" contract="MyNameSpace.IService1" />
<endpoint address="Service2" binding="basicHttpBinding" contract="MyNameSpace.IService2" />
</service>
</services>
each contract is implemented by separate service let's say Service1 and Service2 then your configuration will look like :
<services>
<service name="MyNamespace.Service1">
<host>
<baseAddresses>
<add baseAddress="http:localhost:8081" />
</baseAddresses>
</host>
<endpoint address="Service1" binding="basicHttpBinding" contract="MyNameSpace.IService1" />
</service>
<service name="MyNamespace.Service2">
<host>
<baseAddresses>
<add baseAddress="http:localhost:8082" />
</baseAddresses>
</host>
<endpoint address="Service2" binding="basicHttpBinding" contract="MyNameSpace.IService2" />
</service>
</services>
Yes, but exposing those service is a different thing, you can't expose two contracts on a Single address(except if you wrap them in one single interface, not recommended). You need to give different endpoints to the two service contract in your web.config. Forexample,
<services>
<service name="TwoContracts.Service1">
<endpoint address="" binding="basicHttpBinding" contract="TwoContracts.IService1">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://yoururl.com/TwoContracts/Service1" />
</baseAddresses>
</host>
</service>
<service name="TwoContracts.Service2">
<host>
<baseAddresses>
<add baseAddress="http://yoururl.com/TwoContracts/Service2" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding"
contract="TwoContracts.IService2" />
</service>
</services>

Silverlight WCF web service hosting on domain.com and www.domain.com

There's so much stuff on this online but no one seems to be able to answer this... Hopefully someone here will be!
So i have a WCF web service hosted at godaddy.com. Everything works great when i try accessing it using:
http://**www.**domain.com/DataService.svc
problem is when i remove the www i.e.
http://domain.com/DataService.svc
Here's my web.config servicemodel section:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="DataServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="DataServiceBehavior"
name="DataService">
<endpoint address="" binding="basicHttpBinding" contract="IDataService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
Here's my ServiceReferences.ClientConfig
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDataService" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.domain.ca/DataService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IDataService" contract="Web.IDataService"
name="BasicHttpBinding_IDataService" />
</client>
</system.serviceModel>
My service is using a CustomServiceFactory
public class CustomServiceHostFactory : ServiceHostFactory
{
/// <summary>
/// A custom method to eliminate multiple base addresses from the IIS host creation process
/// </summary>
/// <param name="serviceType">The service type to be created</param>
/// <param name="baseAddresses">A list of the base addresses</param>
/// <returns>A service host</returns>
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
if (baseAddresses.Length > 1)
{
ServiceHost customServiceHost =
new ServiceHost(serviceType, baseAddresses[1]);
return customServiceHost;
}
return new ServiceHost(serviceType, baseAddresses[0]);
}
}
Basically what i would like is my WCF webservice to be reachable whether the user enters domain.com or www.domain.com into his (or her) browser.
Any help will be greatly appreciated!
ps. Running IIS 7 with ASP 3.5
Thnaks!
Simon
You're running across a cross domain issue. See here for a list of reasons as to why it happens. As it turns out "www" and no-www are different domains even though they don't seem like it. You'll need to add a cross domain file. See Tim's blog for good info

Resources