简体   繁体   中英

.NET Service Host Calling Its Own Service

I have multiple WCF services self hosted in a windows service. One of these WCF services needs to call another one of the WCF services hosted in the same windows service. This may need to run on the same machine or on the same windows service installed on another machine. Do I need the application to add a reference to itself or is there an easier way of calling one of it's own services. I know how to do the different machine bit by changing the endpoint address but am not too clear on whether I should be adding a reference to itself or not. ie do I need to use the same code that I would use from a separate client.

The concept here is called "location transparency". That is, there is no (technical) difference in calling a (WCF) service that lifes in the same process or on another machine.

Generally, this is considered a good thing, since you could change the location of your services after/during deployment, depending on your needs (stability or resource consumption of individual services).

You could optimize for the fact that you are running on the same machine by configuring the Named Pipe Binding - whether that actually makes any noticeable difference of course depends on how long your service operations run anyway, doing their actual task (see here for more/good information on choosing an appropriate binding).

Finally, if it really matters, you could create your own binding , possibly taking advantage of the fact that both services life in the same process - most likely not a trivial task.

At any rate, however, you want to make sure that the actual service implementations have no dependency on the transport or binding being used, so to hold up the location transparency.

If you want to access a WCF service, regardless of whether it's hosted in-process or not, generating a proxy for it is a good idea and an easy way of accessing it.

It's not necessary, though, you can create a Channel to the service yourself using ChannelFactory . But if you can, why not generate a proxy for it?

You can just create a new instance of the service with new keyword (targeting the concrete implementation of the service - not the interface, of course). I do this all the time and it works fine. Also, a project can't add a reference to itself or that would cause a rip in the space time continuum ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM