First please tell me is this solution structure is good as far as Architecture is concern?
I have XXXX. Business project in center.
XXXX. Contract project has a reference of it.
XXXX. Service project has reference of Contract and Business project.
Now i want my service to be hosted on flexible environments. That's why I want to put custom hosting logic in the Service > Host folder. This project will also have Custom Instance providing facility so that My service classes can be created with some parametric constructor.
Also I need to have different kind of endpoints so I also have a folder for Bindings
These 3 custom things currently suffice my requirement.
Now please guide me with some sample code snippet for Bindings/Host/Instance
I do not know what you mean by
i want my service to be hosted on flexible environments
and
I want to put custom hosting logic in the Service > Host folder
The type of hosting container used depends on the type of project (web, console, windows service, etc) which the service implementation is reference in. This is not something you want to bundle into one project, you should have a different project (or even a different solution) for each of your different service instances.
And this leads to your general solution structure. By placing the contracts as a project in your solution, you are coupling the contract assembly build (and potentially, deployment) to the build and deployment of your solution. The contracts should ideally be in their own solution so that they can be built and managed separately from your service implementation. What if at some time you need to maintain multiple versions of your contracts?
I think your approach to create a generic service which can be anything to anyone is way to complex. You should let WCF take care of this kind of work for you, create a different project at least for each of your service implementations, and defer the bindings management to deploy-time.
Additionally, unless you are writing your own custom bindings code you will not require a folder for bindings.
Bindings can (and should) be defined in configuration when you ship your endpoint, and to some extent, the decision about which transport binding to use should be an administration or management rather than a development concern.
Related
I have a solution where I add a service reference for a service and the service shows up as a service reference and everything works great. Very Happy.
In a separate solution I have created the service. Within this solution I have a project for unit testing the service. When I add a service reference for the service to the unit test project, the reference shows up as a connected service rather than the expected service reference, and I do not have access to classes specified by [DataContract] .
This behavior is not the same as installing the service on a different project, and it is preventing me from testing the service.
To recap, I have one service, installing it in two separate solutions produces different results. One solution the install works as expected and I have access to classes specified by DataContract and the other solution Installs the service and I do not have access to classes specified by DataContract. Both installed services provide all the methods exposed in the interface.
What can I add to help figure this out? I am not terribly familiar with services so please ask me questions to help improve this question.
****UPDATE****
What is working if I add a new project, then the service provides the data contracts. Something is going sour when I am updating the service references on an existing project. Obviously creating a new unit test project and moving everything in is a pain.....
I solved this issue by a combination of restarting visual studio, restarting the computer, deleting the service reference, re-installing the service reference. I believe deleting the service reference then reinstalling using a different namespace ended up working. I also deleted the unit tester and rebuilt it, re-doing all references, installing the service under a new namespace, so that may have been the total fix.
****Update it broke again and this did not work*****
I am working on a project in which I want to use a Windows Workflow 4 State Machine. The Visual Studio solution templates and most guidance seem to steer everything towards hosting as a service in IIS that is created dynamically from send and receive activities within the workflow.
However, I would prefer to not use the send and receive activities and then host in my own WCF service which would allow me to use a Windows Service instead of IIS and use other bindings like TCP instead of HTTP and create my own interface instead of exposing MEX. In addition, it would be portable to any other hosting arrangement like in a WPF app or a console or whatever.
This feels a lot more flexible to me. Somehow, having service operations as part of the workflow seems like pretty tight coupling of two things that aren't that related. Is there any downside to my approach? I'm new to WF so I might be missing something.
Depending on the kind of workflows you are running you might need to write quite a bit of pluming code that workflow services provide for you.
Things to consider:
Are your workflows long lived?
Are you sending multiple messages to the same workflow?
Do your workflows need to survive a host restart?
Are you using Delay activities to respond to timeouts?
Do you need to be a able to retry action after error situations?
Lots of these things are automatically taken care of with a WF service and need your attention otherwise. It is certainly doable, I have done it in the past, but be aware of of what you are losing.
I'm new to WCF, so I opened up 2 projects: WCF class library and a host console application.
Now, both projects have app.config to store the WCF service configuration settings.
As it seems to me now, and correct me if I'm wrong, I have redundancy in configuring both projects with WCF settings.
How is it done in real world production software ? Does it use separate *.dll library for WCF services, or is it implemented withing the host project (and by that use a single place configuration of it) ?
Thank you .
EliorCohen's answer is correct, but I wanted to expand on a couple of points.
First, your building a WCF class library - library's don't use configuration files on their own. They use the configuration file of the calling application. This is something that I've seen cause a lot of confusion for developers, especially if they create a new class library and they see an App.config file in the project.
Second, with WCF 4 you can actually host a service without specifying anything in the configuration file. The runtime will add default endpoints based on the URI's supplied when you construct the service host.
You can also use set up default bindings and behaviors that will override the normal defaults - for example, if all of your services would be handling large requests, you might want to define a default binding with larger values (by ommitting the name attribute in the Binding configuration).
WCF is wonderful in that it has a lot of options - but that blessing is also a curse at times, especially when you first start working with it.
For more information on default endpoints and stuff, see A Developer's Introduction to Windows Communication Foundation 4.
Note that you'll still need a configuration file for any client apps.
the wcf project you are building represent an implementation of the service.
the configuration need to be on the host of the service (your host app).
In article after article after article, any discussion of using an IOC Container (Unity, Windsor, etc.) with IIS involves the creation of a custom ServiceHostFactory and a custom ServiceHost.
The only reason I can see for that, is so that a custom service behavior, with an IInstanceProvider-related payload, can be applied to all services. So I'm trying to understand why the whole affair is not simplified by having an anonymous service configuration. Such configuration would allow a custom behavior to be applied to all services without using a custom ServiceHostFactory and custom Service Host.
That said, the only reason I can imagine a custom service host would be necessary is if the custom IInstanceProvider is recycled with each WCF instance or context instance. Certainly I would want IOC Container bindings to be established only once for each launch of my IIS ServiceHost rather than repeatedly or irregularly.
If my custom IInstanceProvider is indeed being recycled sporadically, then I could put my IOC Container into a custom service host - to insure that it will stick around as long as possible.
But again, if my custom IInstanceProvider will last as long as the in-built service host, then why not just skip the custom service host factory and custom service host?
In fact, taking this a bit further, if I were to put my IOC Container into a static member of my custom IInstanceProvider, then it wouldn't matter if the IInstanceProvider was being recycled irregularly. It comes full-circle: why do I need or want a custom ServiceHostFactory and custom ServiceHost to use an IOC Container with WCF?
Well. That's because most IoC/WCF integrations (like mine) does more than just creating your service (with it's dependencies).
Custom lifetimes / Disposal
Everything should get clean up properly when a request have ended. Your servcice and your IoC classes uses different lifetimes.
The end of a request can't be detected just with the help of an InstanceProvider.
IContractBehavior / IServiceBehavior
You can register behaviors in the container and automatically get them added to your WCF service.
Personally, i'm a fan of using a design where you don't need any ServiceHostFactory, ServiceHost, ServiceBehavior, and InstanceProvider combination and where you don't have to register the factory in your .svc file. I like my WCF service as just a really thin layer on top of a message passing architecture, and this saves you from using this WCF integration stuff. You can read more about such a design here.
Interesting question! The only reason I could think of for doing it this way is that you don't want to clutter up your app.config with items that are never going to change (i.e. the IoC container used by your WCF services). I try to restrict my use of the app.config to things that an end-user / developer might conceivably want to change without recompiling (in most cases, not much). This makes it more readable, and it is usually easier to specify configuration programmatically as there is better support for intellisense and compile-time checking within program code as opposed to configuration files.
However, this argument might not apply to the first article you linked to, because they're actually setting up their IoC container through the app.config. So it might be a double standard in this case. I've never really understood why people want to set up all their dependencies through external configuration files anyway. Storing static configuration in the app.config just seems like overkill to me in most cases.
I would like to create a service whose job is to monitor other services that are running within the same process, and then report basic information like health or service dependencies. I'm having trouble figuring out the best way for my monitoring service to access detailed information about the other services without having to have each service publish its metadata or expose some custom endpoint the monitoring service can communicate with. If I load the configuration and read through it I can get most of the way there but this approach has a few weaknesses:
Getting the absolute URI for each endpoint can be difficult,
especially when using IIS hosting or fileless activation.
Any configuration that was done programmatically would not be able to be read by the monitoring service
What I'd like to be able to do is to somehow access the ServiceDescription to get all the information I need about each ServiceHost, without requiring any work on the part of the service designer to give it to me. Is something like this possible?
If you've checked Channs links and are convinced you need to roll your own health monitoring infrastructure, you'll probably need to either derive from ServiceHost or go all out and derive from ServiceHostFactoryBase or possibly do both depending on what you need to implement. They'll give you access to the ServiceDescription instance for each service as it is spun up.
One alternative would be to use WCF's built-in health monitoring and performance monitoring capabilities. This works at the individual service level though.