I have a WCF service (INTWCF) that consumes another WCF service (EXTWCF). EXTWCF exposes 5 operations, will be hosted on an external app server (in a DMZ), and implements message and transport level security.
INTWCF will be hosted on an internal app server, does not implement any security, hosts two individual services with approx 30 operations - a number of which are called by the operations on EXTWCF (along with various other domain level applications), depending on various parameters passed in (EXTWCF contains simple logic to determine which operations on INTWCF should be called).
EXTWCF implements INTWCF using IoC and DI.
Using TDD, I would like to write my initial unit tests for the operations exposed on EXTWCF. I would therefore like to mock up INTWCF using Moq. I would have thought that I should mock up and inject INTWCF in to the unit testing project, but I've read (in quite a few places) that IoC and DI should not be used during unit testing due to the additional testing dependencies they introduce.
Am I being fed incorrect information, or is there another way to approach this problem? Is mocking appropriate for this situation? Seeing as though my unit tests will be accessing the operations on EXTWCF, they will not know about INTWCF. This seems to me to be a perfect case for DI?
I'm using Ninject for IoC and DI; if DI is the answer, does Ninject provide a bootstrapper / plugins for unit testing? I'm not familiar with any, and don't see anything on their web page?
Related
My team maintains a very large .Net Core 2.1 web site. Lots of controllers, lots of services that get injected into the controllers via constructor injection.
Sometimes due to developer error a service class is no longer added to the DI container during startup. Obviously this leads to an exception when MVC tries to construct a controller that relies on that service (in response to an incoming request).
Problem is that this may affect only some lightly used controller, so our (far from perfect) regression testing doesn't pick up the regression bug. But it is still bound to be picked up by one of our (very demanding) customers.
I though of writing a unit test that would
Instantiate a ServiceCollection class (that implements IServiceCollection);
Call our own method that adds all services to that service collection (the same method used during normal startup);
Find all controllers through reflection, and try to construct them the same way MVC does - by getting dependent services from the DI container.
So my question is:
Does this approach make sense?
Is there an example somewhere that I could use?
Failing an example, how would I achieve 1) and 3) ?
What are the advantages and disadvantages of having more WebMethods in a single Web Service?
If you add multiple web methods to a single web service class, they will all be supported through a single URL (which can make deployment and configuration much simpler). On the client side, when you create a web reference, it will create a separate class for each web service, and than separate methods within each class, one for each web method. So, as you can imagine, it's easier to work with in the client code, as well, when you have a single class with multiple methods rather than multiple classes each containing only one method. That can become a nightmare for dependency injection (DI) (you are injecting your dependencies right? Hmmm?). So, the rule of thumb should be to try to group all the related methods together into a single web service and keep the number of web services as few as is reasonable.
I have an class library called ServiceLayer which acts as a repository for a ASP.NET MVC application This service layer has a references to a WCF Service called ProfileService which contains Profile methods to perform CRUD operations on a database etc.
I now need to allow mobile devices to communicate with my application so I have created another WCF Service called ProfileService. This service has a reference to the ServiceLayer class library and makes calls to it to undertake Profile operations.
Now this is quite confusing as I now have 2 ProfileServices. The first communicating with my database etc and exposing itself to my service layer. The second communicating with my service layer and exposing itself to mobile devices.
What is the best way to name your services in a SOA environment to avoid confusion of which type is which? especially when mapping between types.
I may also want to create another service which acts as an API to users of the system. What would I name this service ProfileAPI?? I know each ProfileService is in its own namespace but this doesnt help with readability when creating AutoMapperSettings or performing manual mapping.
So if anybody out there knows of a good way to name services in this environment it would be much appreciated.
You are looking for a Service Facade
You would end up with a Facade, which is just a specialized interface into your real service. You would define the different services as needed (mobile, users, database)
I have a WCF server that is a library assembly. (I am writing it so I can mock the level below it) It is called var a client helper class that is in a different assembly. As the data that is transferred is complex and the server has to send call-backs to the clients I wish to test the WCF code in isolation.
(I am only interested in the TCP channel or NamePipe channel)
I do not wish to mock WCF, as the risk I am trying to control is my usage of WCF.
It there a easy way to
Load my WCF server into a different app domain
(I could load the WCF server into the main app domain, but then I it harder to prove that the objects were serialized correctly rather than just pointer moved about.)
Setup all the WCF config so the client class can call it (most likely named pipes or TCP)
And use it in some nunit test
I rather not have my unit tests depending on config file.
I expect (hope) that there are some util classes for setting up WCF unit test that I can just pass the type of my server class to and will give me back a client factory that connects to the server.
Am I going about this the wrong way, e.g there a better way of testing my communication layer and usage of WCF?
It is by far the easiest approach if you spin up the service in-proc, because then you don't need to write a lot of complex synchronization code to determine when the service is running and when it isn't.
Don't worry about pointers being passed around - they won't (unless you choose the new in-proc binding in WCF 4). It's the binding that determines how and if objects are serialized. Named pipes are excellent for this purpose.
I always spin up a new ServiceHost in each test case inside a using statement, which effectively guarantees that the host is running before calls are being made to it, and that it is properly closed after each test. This last part is important because it ensures test independence.
You may also want to look at a series of blog posts I wrote about a very similar subject.
You can use SOA Cleaner for testing your WCF. Take a look at http://xyrow.com
no installation is needed. It's not unit testing, but it can be very helpful (you can have it run on your build, as it supports command line too).
I'm new to WCF and in a large part, also distributed programming. I am working on a project that requires 4 discrete services.
I am trying to correctly define the responsibilities for each component of each service. Suppose I have service B which needs to communicate with service A. For each service, I have defined the service implementation class, the service host, and a proxy class.
In order to unit test, I am using dependency injection - since service B needs to communicate with service A, I have passed an instance of A's proxy class as a constructor argument to service B.
When I am unit testing service B, I must have A's service host up and running.
Is this the wrong way of going about dependency injection? If so, why, and how do you recommend I do it?
Is there a better way of going about dependency injection?
Should I have to run the service host to get the right results in the unit test?
Consider using
ChannelFactory instead of generated clients.
ChannelFactory<IHello> clientFactory = new ChannelFactory<IHello>("targetConfiguration");
IHello client = clientFactory.CreateChannel();
string result = client.SayHello();
Interface types wherever possible
one of the mock object frameworks (example) to inject interface implementations when writing your tests.
Regarding your third question, the answer is "No" if your aim is testing particular small units (the whole point of unit testing :). But it's always better to write some integration tests to make sure you don't have any serialization/hosting problems.