WCF: Windows service cannot find endpoint when hosted in Winforms application - c#

I require a Windows Service to make WCF calls to a service hosted in a WinForms application.
Unfortunately when attempting the call the Windows Service fails to discover the Endpoint.
I have tried changing the Log On properties for the Windows Service to allow interaction with the desktop, however this did not help.
I have used the exact same hosting code (as used by the WinForms app) in a Console application and the Windows Service finds the Endpoint no problem.
Any help would be much appreciated...
Code to host service in WinForms app.
_myServiceHost = new ServiceHost(typeof(MyService);
_myServiceHost.AddServiceEndpoint
(
typeof (IMyService),
new NetNamedPipeBinding(),
#"net.pipe://localhost/MyService"
);
_myServiceHost.Open();
Code from the client proxy...
_serviceFactory = new ChannelFactory<IMyService>
(
new NetNamedPipeBinding(),
"net.pipe://localhost/MyService"
);
...
IMyService clientProxy = _serviceFactory.CreateChannel();
clientProxy.SomeMethod();
This problem does appear to be to do with the security context in which windows services run that is preventing the Endpoint hosted by the WinForms app from being visible to the service but not vica versa.
UPDATE:
I tried changing the binding from NetNamedPipeBinding to NetTcpBinding and it seems to work fine with this type of binding.

Are you sure that you have the app.config in its place and you have opened the service host?
The issue must be in that. And windows service has no role here, check it with wcftestclient and also try to view the wsdl (if enabled) in web browser.
But mainly I believe you have not opened the host :
ServiceHost host = new ServiceHost(....);
host.Open(); // check is it successfully called

Related

Dynamically consuming WCF in Windows Phone 8 App

I have a WP8 app and this app needs to consume a WCF service to send/receive data from my server.
I have the service and the app working well in my developer computer, using localhost and VS2013.
Now I installed the WCF service in IIS and the service is working well!
My question is: I want a way to dinamicaly change the address of my WCF service without the need to recompile the app and deploy it!
I've found this peace of code in another thread at SO that I would like to know if it would work in any address that I change in my app dinamycally:
private MyServiceClient GetMyServiceClient(string url)
{
Uri uri = new Uri(url);
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
EndpointAddress address = new EndpointAddress(uri);
MyServiceClient client = new MyServiceClient(binding, address);
return client;
}
My app will run for now just in intranet enviroment, so I don't need a high security level to access my server, the basic httpbinding it's for now good enough.
You can look at putting your wcf configuration into a configuration file. See the post here - http://msdn.microsoft.com/en-us/library/ms733932(v=vs.110).aspx
Also take a look at this link - http://www.codeproject.com/Articles/576820/Basic-Step-by-Step-WCF-WebService
It gives an example of consuming a wcf endpoint that is defined in a config file. It uses a wpf example, but it should be easily adaptable to your scenario.

C# soap web service and Java client

I have problems with writing Java client for wshttpbinding web service.
I wsimport-ed .wsdl and i try to
TestService iface = new TestService();
ITestService implmt = iface.getWSHttpBindingITestService();
then i call web service method set
implmt.set("s", 1);
And i get
Exception in thread "main" javax.xml.ws.WebServiceException:
java.net.SocketException: Connection reset
I cannot change server side, it must be as is. So change to basicHttpBinding is not possible (if i change it i don't have any problem but alas)
Client must be made in Java.
Oh ok I solved it. I imported METRO .jar files to build path and moved them to top (to be first)
It is crucial to place those jars on top.

How can I run a WCF service constructor once for all clients when it's InstanceContextMode is set to PerSession?

How can I run the code in the constructor of a WCF Service only once when the ServiceBehaviorAttribute.InstanceContextMode is set to PerSession?
[ServiceBehavior(
InstanceContextMode = InstanceContextMode.PerSession,
ConcurrencyMode = ConcurrencyMode.Single)]
public class SomeService : ISomeService
{
public SomeService()
{
this.RunThisOnceForAllClients();
}
private void RunThisOnceForAllClients() { }
}
Or, how can I make a method run automatically once the WCF Service is running but it will only run once for all client calls?
Please help. Thanks in advance.
I deploy my WCF Service using a Managed Windows Service. My code is in C#. Framework is in .NET 4. Project is build in Visual Stuido 2010 Professional. The service is consumed by a Windows Forms Application. In case you wonder on why do I need to do it like this, I need to execute an Uploader method that will upload the database of the service to another service, but it will be executed in a certain time so I put it in another thread that will always run as long as the service is running.
Why not run this operation just before you host the WCF Service in your windows service so it can be ready as soon as the WCF Service goes online. You can get from the running thread an event that it is finished and then deploy the WCF Service.
You need to write a service behavior or an endpoint behaviour. In this behaviour call the function at first call from a client and set a variable to true and store it in some permament memory or file location.
You may have a look at the following msdn article about Extending WCF
Use a static constuctor? It will be called once when(before) any action with that class is taken in your code.

Connect to Unknown SOAP Web Service

I would like to build an app in C# that connects to an Apache AXIS web service and performs the following operations via SOAP.
Login in to the server.
POST string data to server
Receive and display server response
Here's the tough part. I do not have access to the server, nor do I know where the .JWS file is located on the server. I was able to get to the WSDL file in my web browser, so I know a "Login" operation exists as well as an operation to take in data.
I have tried accessing the web service via URL, but I keep getting this message:
Hi there, this is an AXIS service!
Perhaps there will be a form for
invoking the service here...
In summary, is there anyway I can connect to this web service when all I have is the URL of the WSDL file? Are web services accessible via URL?
Thank you
Use WCF, and generate client proxies to the web service using the svcutil.exe tool.
running svcutil.exe http://url.to/webservice?WSDL the_wsdl.wsdl /language:C# should generate proxy classes you can use in your C# project, and you'd call the service e.g. like
BasicHttpBinding myBinding = new BasicHttpBinding(); //might not even need these
// 2 lines if you ran svcutil.exe directly on the web service URL
EndpointAddress myEndpoint = new EndpointAddress("http://url.to/webservice");
TestClient client = new TestClient(myBinding,myEndpoint); //the generated classes
// svcutil.exe created
client.SomeOperation(42); // call an SomeOperation of the web service
Thanks for everyone's help. Looking back on this question, I can see how severely confused I was. Here is the solution I followed.
Assuming you know the URL of the WSDL file of the service you wish to connect to then just do the following.
Boot up Visual Studio
On the top toolbar navigate to Data -> Add New Data Source then choose Service in the new dialog
In the address bar, enter the URL of the wsdl file (EXAMPLE: http://server.com/services/displayName?wsdl)
Near the bottom of the dialog, change the namespace to something relevant to the project (EXAMPLE: sampleService)
Now Visual Studio should compile the client proxies for you that you can use to access the web services on your server. To access one of the services, all you need to do is create a new object from the class.
//Example
sampleService.ClassName test = new sampleService.ClassName();
test.displayName("Jack");
See http://msdn.microsoft.com/en-us/library/bb552364.aspx for a starting point

WCF: Client Impersonation

I have a console app client that talks to a WCF service hosted by a console app on a different server. It use windows authentication with security mode = message
It works just fine until I change the service to impersonate the clients credentials. The changes I do to accomplish that is:
1. Add <serviceAuthorization impersonateCallerForAllOperations="true" /> to the service behaviour
2. Add [OperationBehavior(Impersonation = ImpersonationOption.Required)] to my method signature
I then host my service and it runs as normal, all good.
In my client the only thing I do is add:
ChannelFactory<IService1> channel = new ChannelFactory<IService1>(binding, endPoint);
channel.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
I then run my client and get the error:
The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.
Is there something else I'm missing that I need to do to the client config?
It could be that your server settings do not match the contract.
On the server side:
Is IIS set to windows authentication?
Is web.config set to windows authentication?
Is web.config set to impersonate = true
My guess is that you are missing the last one.
I would suspect that the wcf method call triggers an exception on the server side. Do you have any infrastructure in place to capture server side exceptions or payloads? If not then use something like wcf trace logging to record traffic.
This should give you a more meaningful error.
If you can add this to your question hopefully we can discern what is causing the issue.

Categories