How to access WCF self hosted service from remote php server - c#

I created the self hosted WCF service with C# application. I want to send messages to C# application from remote php server. How can i perform this function?
<?xml versio="1.0"?>
<configuration><system.serviceModel>
<services>
<service name="MyMathServiceLib.MyMathService" behaviorConfiguration="myMathServiceBehave">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/MyMathService"/>
<add baseAddress="net.tcp://localhost:9002/MyMathService"/>
</baseAddresses>
</host>
<endpoint address="http://localhost:9001/MyMathService" binding="basicHttpBinding" contract="MyMathServiceLib.IMyMathService"/>
<endpoint address="net.tcp://localhost:9002/MyMathService" binding="netTcpBinding" contract="MyMathServiceLib.IMyMathService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="myMathServiceBehave">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
This is my app.config file(ref: http://www.codeproject.com/Articles/650869/Creating-a-Self-Hosted-WCF-Service)
My aim is to send message from php server to c# application

Follow the simple steps:
In your visual studio folder, you should see WCF Client.
Add your WCF URL to this client (http://localhost:9001/MyMathService in your case). Fill example data in the request and click on "XML".
Copy the XML and put it in a variable in PHP. Instead of your dummy values you put in the previous step, put proper PHP variables.
Enable PHP CURL. POST this variable to the WCF service.

Related

How to resolve AddressAccessDeniedException in WCFhost

I created a simple WCF service and hosted in a console application.
After configuring all the endpoints I got an exception as below.
Below in my configuration file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="HelloService.HelloService" behaviorConfiguration="mexBehaviour">
<endpoint address="HelloService" contract="HelloService.IHelloService" binding="basicHttpBinding"></endpoint>
<endpoint address="HelloService" contract="HelloService.IHelloService" binding="netTcpBinding"></endpoint>
<endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8081/"/>
<add baseAddress="net.tcp://localhost:8090/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Help me how to resolve this error?
The error occurs because of permission problem in your operating system.
Your operating system does not allow WCF service on a specified port.
All you need to do is to create an exception for that port.
Below is the procedure:
Click Start
Run cmd prompt as an administrator
type netsh.exe and press enter
type http add urlacl url=http://+:8081/Service/ user=administrator and press enter.
Here you need to observe three points
8081: is your port on which you need access. Replace it with your own port number.
Service: this is your sesrvice name. Ex: MyWeatherService
administrator: you have to give access to the ser you want on your computer. In my case I gave access to adminitrator. Ex: you can give access to the puppy if there is an user named puppy on your machine.
Check first if the WCF service can user port 80
run your application as administrator, it will resolve the issue.

Failed to invoke the service. Possible causes: The service is offline or inaccessible while Invoking

I have my service configured as below:
<system.serviceModel>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="MyServiceTypeBehaviors">
<host>
<baseAddresses>
<add baseAddress="net.tcp://127.0.0.1:808/service" />
</baseAddresses>
</host>
<endpoint address="net.tcp://127.0.0.1:808/service/"
binding="netTcpBinding"
contract="WcfService1.IService1"/>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
</services>
<protocolMapping>
<add scheme="net.tcp" binding="netTcpBinding"/>
</protocolMapping>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="MyEndpointBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
and the client as:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IService1" sendTimeout="00:05:00" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://127.0.0.1/service/" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IService1" contract="IService1"
name="NetTcpBinding_IService1">
<identity>
<servicePrincipalName value="host/MachineName" />
</identity>
</endpoint>
</client>
</system.serviceModel>
When using WCFTestClient or SVCutil, I am able to discover and access the servie and generate proxy or stubs.
But when I want to invoke any of the methods getting following error:
Failed to invoke the service. Possible causes: The service is offline or inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service.
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:04:59.9980468'.
Set the following value inside the endpoint block
<identity>
<dns value ="localhost"/>
</identity>
Maybe not the answer you are looking for, but for development on the local machine I always use an HTTP endpoint. I find Net.TCP will only play nice when it is hosted on a server in IIS.
It is easy enough to add the NET.TCP endpoints once you deploy to an IIS server. Note: that the base address no longer has any affect as that is set in IIS. To test the new endpoints it is best to start by opening the service in a browser while remoted into the server, that way you get the best error messages.
If i understand correctly you are trying to select the endpoint when creating the service reference? Thats now how it works.
If you create your service reference using 127.0.0.1/service/Service1.svc
You should see in your config file something like the following. (When I create my service endpoints I always name them with the protocol as a suffix.
<endpoint address="http://servername:8090/servername/servername.svc/Business"
binding="basicHttpBinding" bindingConfiguration="BusinessHTTP"
contract="servername.IService" name="BusinessHTTP" />
<endpoint address="net.tcp://servername:8030/Service/Service.svc/Business"
binding="netTcpBinding" bindingConfiguration="BusinessTCP"
contract="servername.IService" name="BusinessTCP" />
then in your code you can choose which endpoint to use.
Dim svc as New SeviceReferenceName.BusinessClient("BusinessTCP")"
I have faced the same issue. It was the issue of port address of EndpointAddress. In Visual studio port address of your file (e.g. Service1.svc) and port address of your wcf project must be the same which you gives into EndpointAddress. Let me describe you this solution in detail which worked for me.
There are two steps to check the port addresses.
In your WCF Project right click to your Service file (e.g. Service1.svc) -> than select View in browser now in your browser you have url like http://localhost:61122/Service1.svc so now note down your port address as a 61122
Right click your wcf project -> than select Properties -> go to the Web Tab -> Now in Servers section -> select Use Visual Studio Development Server -> select Specific Port and give the port address which we have earlier find from our Service1.svc service. That is (61122).
Earlier, I had a different port address. After specifying the port address properly, which I got from the first step, EndpointAddress 61122, my problem was solved.
I hope this might be solved your issue.
NetTcp binding is secured by default.In the security authentication process between client and service,WCF ensures that the identity of service matches the values of this element. Therefore service need to be configured with :
<identity>
<dns value ="localhost"/>
</identity>

500 Internal Server error while accessing WCF service from http and unable to add service reference for the same reason

I am trying to access my WCF service that resides on a sub-domain on my website. The properties of server on which sub-domain is situated is as follows:
It has basic authentication
It has 4.0 .NET version
But the problem is, when I try to access the service like http://mysubdomain.mydomain.com/Service1.svc it gives me
500 - Internal server error.
Although it is working fine from localhost. I also changed the properties in the web.config as:
<services>
<service behaviorConfiguration="WCFService.Service1Behavior"
name="WCFService.WrangleCore">
<endpoint address="http://mysubdomain.mydomain.com/ServiceCore.svc" binding="basicHttpBinding" contract="WCFService.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFService.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>
Please tell me the solution to this.
It is worth checking if your DNS suffixes are correctly configured. I have seen it to due to a silly spelling mistake as well!

WCF Windows Service consuming Service embedded in Windows Form

I have a Windows Service with a WCF Library. On the same machine, I have a Windows Form application with a WCF Service embedded into it. My problem arises when I try to consume the service in the Form from the Windows Service.
The WCF service in the Form has a "basicHttpBinding" endpoint exposed as well as a "mexHttpBinding". I am able to test the service in the Form with the 'WCF Test Client' with no problems.
In the Windows Service, I am able to add a Service reference to the Form's service and all is well. However, when I actually run the Windows Service, and try to consume the WCF Service in the Form, I get this error:
The message with Action 'http://tempuri.org/IService1/shutMeDown'
cannot be processed at the receiver, due to a ContractFilter mismatch
at the EndpointDispatcher. This may be because of either a contract
mismatch (mismatched Actions between sender and receiver) or a
binding/security mismatch between the sender and the receiver. Check
that sender and receiver have the same contract and the same binding
(including security requirements, e.g. Message, Transport, None).
See that "IService1" in there? I'm pretty sure that's the problem. The Windows Service itself is configured to host a Service called "Service1" as you can see below in the <services> section. So it seems like I must have something wrong in my app.config.
My best guess is that I have something wrong with the way that I have my <client> setup in my app.config. It seems like, for some reason, when I am trying to consume the service in the Form, the Windows Service is using the wrong address. It's using the address of it's own service, not the address of the Form's service.
Here are the relevant parts of the app.config from my Windows Service:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IFormControlService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8700/FormControlService"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFormControlService"
contract="Form_ServiceReference.IFormControlService" name="BasicHttpBinding_IFormControlService" />
</client>
<services>
<service name="smMonitor_wcfServiceLibrary.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8800/smMonitorService/" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="smMonitor_wcfServiceLibrary.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
My setup is this. I have a Windows Service running on a machine. On the same machine I have a Widows Form. The Windows Form exposes a service that will allow the Windows Service to interact with it.
My problem was as follows. I only had the outgoing <bindings> defined. What I needed to do was to define both the incoming and outgoing bindings like this:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
<binding name="BasicHttpBinding_IFormControlService" />
</basicHttpBinding>
</bindings>
Since the outgoing bindings were not defined, my Windows Service was looking in the <basicHttpBinding> and just using the default one.

WCF, changing the baseAdress of an endpoint

i have a few questions about the below config file:
<system.serviceModel>
<bindings />
<services>
<service behaviorConfiguration="WcfReporting.Service1Behavior"
name="WcfReporting.Service1">
<endpoint address=""
binding="basicHttpBinding" bindingConfiguration=""
contract="WcfReporting.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:5050/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfReporting.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>
Why when I hit F5 to restart the service, the service starts with this URL http://localhost:2752/ ... why not 5050 as I specified in baseAddresses.
How how can I add another endpoint. I tried with endpoint address="/Address2" binding="basicHttpBinding" contract="WcfReporting.IService1" />
should I how be able to access the service, not only with http://localhost/VirtualDir/ but also with http://localhost/VirtualDir/address2 or how does it work?
If you're hosting in Cassini from within Visual Studio 2005 or later, you can specify the port using Project/Properties/Web/Use Visual Studio Development Server/Specific Port.
By default the port will be auto-assigned - which isn't very helpful for Web Services as your clients will probably want to be using a fixed URL.
You don't need to specify <baseAddresses> in the config file when hosting in IIS or Cassini - the base URL is provided by the web server. The <baseAddresses> element is used when self-hosting.
How how can I add another endpoint. I
tried with endpoint
address="/Address2"
binding="basicHttpBinding"
contract="WcfReporting.IService1" />
The addresses you specify in this endpoint need to be local and relative - e.g. just specify
<endpoint address="Address2"
binding="basicHttpBinding"
contract="WcfReporting.IService1" />
and this will create an endpoint at the complete address of
net.tcp://localhost:5050/Address2
But as Darin has already pointed out - if you use IIS / WAS to host your service, the virtual directory where your *.svc file resides will take precedence and the base addresses specified will be ignored. In order to really use the base addresses, you'll need to self-host the service in a console app or Windows service.
Marc
If you use a web server (such as Cassini or IIS) to host your WCF service the base address will be provided from this server. Also note that you cannot use TCP bindings over HTTP. If you want to be able to set the base address property you need to host the service yourself (for example in NT service, console or windows application).

Categories