So I created winforms client and added wcf class library to the solution.
In winforms I do
ServiceHost svc = new ServiceHost(typeof(...), new Uri("net.pipe://localhost/MyNamedPipe")
and then svc.Open() which executes fine.
Now, how do I add a service reference so in same winforms I can get proxy for that wcf?
I only was able to generate that by using ASP.NET Development Server which started when winforms was ran and so I copied that url, stopped debugging (Development Server was still running) and then added a service reference from there. But that isn't correct I guess.
Of course I can reference wcf contract class directly and use it, but that is not proper either.
When you are controlling both ends like that, I prefer to use ChannelFactory:
NetNamedPipeBinding binding = new NetNamedPipeBinding();
EndpointAddress address = new EndpointAddress("net.pipe://localhost/MyNamedPipe");
ChannelFactory<YourInterface> factory = new ChannelFactory<YourInterface>(binding, address);
YourInterface yourInterface = factory.CreateChannel();
Have you tried adding a Service Reference... to the project, then entering your URI directly in the Address box of the dialog?
Note that this should be the complete URI, such as net.pipe://localhost/MyNamedPipe.
You can find step-by-step instructions from MSDN here.
Related
How to setup a proxy in c# code for Google Text-To-Speech API.
Does somebody knows where to put in the proxy settings for the Google TTS API in c#. Our project runs locally but not on the server behind a firewall, so it has to go via the proxy.
Hope you have a starting point for me ;-)
Thanks!
The intention is that if you've set the system proxy, or the HTTPS_PROXY environment variable, that should just work by default.
However, at the moment there's a bug in Grpc.Net.Client that causes that to fail. (Once it's been fixed and released, we'll obviously update our dependencies for the next release of Google.Cloud.TextToSpeech.V1 and other Cloud libraries.)
The simplest workaround is probably to still use the Grpc.Net.Client implementation of gRPC, but with an additional option:
var client = new TextToSpeechClientBuilder
{
GrpcAdapter = GrpcNetClientAdapter.Default.WithAdditionalOptions(
options => options.HttpHandler = new HttpClientHandler
{
Proxy = new WebProxy("your proxy here"),
UseProxy = true
})
}.Build();
I've created the WCF service and some simple WPF application consuming it. When I'm running the project from within Visual Studio, the WCF Test Client opens and the application works just fine, method defined in service work.
But I need to host this WCF service in a Windows Service. I've followed this, installed the services using Installutil.exe and the ran the service. Everything went fine, it's working.
Yet, when I'm trying to open the executable file with WPF application directly from the debug folder of the app, I'm getting this error:
zad8. has stopped working
After choosing the option to debug it with new instance of VS I get
XamlParseException occured in PresentationFramework.dll
The stack trace shows something like:
connection can't be started, because the target computer is actively refusing it
Do you have any idea what could go wrong?
Fortunately, I've managed to come up with solution. I think I should post it, maybe one day it will help somebody:)
I actually did two mistakes, but one of them was unfortunately caused by the mentioned tutorial (here) in connection with my temporary blackout.
In step 5, point 8 of this tutorial, there's an example of overriding OnStart() method:
protected override void OnStart(string[] args)
{
if (myServiceHost != null)
{
myServiceHost.Close();
}
myServiceHost = new ServiceHost(typeof(Service1));
myServiceHost.Open();
}
Beware, that Service1 is ambiguous in this context, because it's name of the Windows Service project class as well as the name of WCF Service class. It should be written with fully qualified name (here it is WcfServiceLibrary1.Service1). In my case, the service name was different, and I just put the Service1 in there in a hurry. Anyway..
In case, someone has it all behind and still encounters the same problem (with app stopped working), I think that you should try open the project in Visual Studio and try to debug the client consuming application as a new instance (right click on the project-> Debug -> Start as new instance...).
It might seem trivial, but when u hit F5 or Ctrl+F5 then even if u have only those project set as startup project, VS will host it's client anyway. In my case it did matter, because I needed to use isolation storage file. And as it was kept on the service side, then I had this file created in IIS server created by VS. Somehow, my method of creating such file had set FileMode.Open() and it was causing the crush, because in Windows Service it didn't exist and the new one couldn't be created and that was neccessary to run it correctly.
What's more it just showed me that this question couldn't be answered properly, cause the data I've provided was not enough and it was delicate.
Cheers:)
I were studying WCF here http://msdn.microsoft.com/ru-ru/library/bb386386.aspx and I successfully did Testing the Service step. However in Accessing the Service step I faced problems. It builds without any error, but when I tried to write smth to textLabel space and pressed button1 I get the error in button1_Click function, namely
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
Error message
Could not find default endpoint element that references contract >'ServiceReference1.IService1' in the service model client configuaration section. This might be because no configuaration file >>was found for your application or because no end point element matching this contract could >>be found in the client element.
I find such code in the app.project file
<endpoint address="http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService11"
contract="ServiceReference1.IService1" name="BasicHttpBinding_IService11" />
I`m 100% sure, that code is without any error, because I copied it from the above site without any modification. So I will be glad to hear your assumptions how fix that.
You should specify the name of the endpoint when constructing the client:
using (var client = new ServiceReference1.Service1Client("BasicHttpBinding_IService11"))
{
client.SomeMethod();
}
or use * if you have only one endpoint in the config file:
using (var client = new ServiceReference1.Service1Client("*"))
{
client.SomeMethod();
}
The reason you need to specify the name is because you could have multiple endpoints (with different bindings for example) for the same service in the config file and if you do not specify the name the framework wouldn't know which endpoint you want to invoke.
Also notice how I have wrapped the IDisposable client in a using statement to ensure proper disposal once you have finished working with it.
I've been banging my head against a brick-wall the past 2 days trying to successfully use this example on MSDN:
How to: Host WCF in a Windows Service Using TCP
If you follow the example through and place the Consumer application within the same solution then it runs successfully - even if the service is turned off !
If the service is switched on and the consumer application is published to say a different network drive then it will not run because the WCF is not listening.
The following piece of code in the walk-through is the culprit:
protected override void OnStart(string[] args)
{
if (myServiceHost != null)
{
myServiceHost.Close();
}
myServiceHost = new ServiceHost(typeof(Service1));
myServiceHost.Open();
}
My friend noticed that if we change new ServiceHost(typeof(Service1)); to new ServiceHost(typeof(WcfServiceLibrary1.Service1)); then the WCF will actually start to listen!
Easy to see it's confusion as there is a class called Service1 within the windows service project aswell as the WCF project so the full qualification is required.
Did anyone else encounter this?
I had the same issue (as you know from the original post in stackoverflow).
Alternatively, you can rename the class Service1 in the namespace WcfServiceLibrary1 to avoid conflicts.
you have to put using WcfServiceLibrary1; at the top
I also found that the the article references the wrong Service Name. When I tried it I had to use:
net.tcp://localhost:8526/Service1/mex
in step 8 instead of what was listed.
net.tcp://localhost:8526/Service1
I have created a class library that should connect to a WCF endpoint project I am hosting.
The client project has commandlets defined that should interact with the service.
However, I keep getting the following error:
Could not find default endpoint element that references contract Service1.MyService 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.
Do you know what the problem might be?
EDIT
What I have is a single Class Library defining cmdlets. I use an .psd1 file to Import-Module which uses the dll files produced.
EDIT2
Once again, I don't have a project referencing my library. It is the powershell that calls the commandlets defined and these cmdlets should connect to the WCF endpoint
Thanks
have got this to work: here is a clean solution:
internal static class ServiceClass
{
internal static Object GetWCFSvc(string siteUrl)
{
Uri serviceUri = new Uri(siteUrl);
EndpointAddress endpointAddress = new EndpointAddress(serviceUri);
//Create the binding here
Binding binding = BindingFactory.CreateInstance();
ServiceClient client = new ServiceClient(binding, endpointAddress);
return client;
}
}
internal static class BindingFactory
{
internal static Binding CreateInstance()
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.UseDefaultWebProxy = true;
return binding;
}
}
create configuration file with endpoints in startup project
When you're running the code in your cmdlet assembly, the executing process is powershell.exe (in %windir%\system32\WindowsPowershell\v1.0 or %windir%\syswow64\WindowsPowershell\v1.0), so any System.ServiceModel configuration will be need to be defined in the powershell config file (powershell.exe.config).
I'm guessing this is probably not a practical option, so you'll probably need to configure your service client or channel factory manually rather than via the application configuration file.
See here for an example: http://msdn.microsoft.com/en-us/library/ms734681.aspx
Another option may be to use an Activation Configuration File: http://msdn.microsoft.com/en-us/library/ff361644.aspx
I'm not sure how well this will work for service configuration elements.