3

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.

Russell McClure
  • 4,821
  • 1
  • 22
  • 22
Nickolodeon
  • 2,848
  • 3
  • 23
  • 38

2 Answers2

4

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();
Russell McClure
  • 4,821
  • 1
  • 22
  • 22
  • Because by adding a reference to your own service, you will just end up with dynamically generated duplicate code. You need the dynamically generated stuff when you don't have easy access to the real interface definition. You have easy access to it so use it. – Russell McClure Dec 07 '11 at 00:40
  • I wrote svc = new ServiceHost(typeof(TokenizationService1.TokenizationService), new Uri("net.pipe://localhost/MyNamedPipe")); svc.Open(); ChannelFactory f = new ChannelFactory( new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/MyNamedPipe")); service = f.CreateChannel(); – Nickolodeon Dec 07 '11 at 00:45
  • But when calling service.Tokenize(value) later I get timeout exception and apparently client doesn't see it. Man, this is tough( – Nickolodeon Dec 07 '11 at 00:46
  • You are attempting to use the main UI thread of your WinForms app to make a call to yourself. You get a timeout because the thread is waiting for itself to get back to the message pump to serve the call. That will never happen, hence the timeout. You need to rethink your design. It doesn't really make sense to try and call your own WCF service form within your that service host. – Russell McClure Dec 07 '11 at 00:54
  • No, Russel. That wasn't the problem. Added serviceMetadata to behaviours solved it. as in http://stackoverflow.com/questions/2579288/mextcpbinding-in-wcf-imetadataexchange-errors – Nickolodeon Dec 07 '11 at 01:12
  • But running on the same thread is also a problem, so I guess you are right. – Nickolodeon Dec 07 '11 at 01:15
1

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.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • I didn't know one can do that. But when I clicked Discover, it gives me services in solution and dicatates me some http://localhost:3049, althought I put netPipeBinding in service config. – Nickolodeon Dec 07 '11 at 00:27
  • @Nickolodeon: Don't click Discover, just enter your full URI: net.pipe://localhost/MyNamedPipe. – competent_tech Dec 07 '11 at 00:35
  • @Nickolodeon: Updated answer with additional info. – competent_tech Dec 07 '11 at 00:36
  • I tried that address too. But why would service reference find it - pipe isn't created at that point, I think. Service needs to be hosted and started, so it create the pipe. But in order to host it, I need to generate proxy classes first. It's like catch 22 (((. Let me check your link though. – Nickolodeon Dec 07 '11 at 00:40