I have a class that represents a WCF service which is used as a member variable in a class that I would like to unit test. This will involve writing a number of scenarios and the problem I have is that the construction of the class a number of times results in a 'registration already exists for URI' error for all of the unit tests other than one.
Any of the unit tests succeeds in isolation, but if I run two or more of the tests in the same test run then I have the problem.
My question is how can I get the unit tests to run in a standard test run where all unit tests are run?
My test code starts like:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var serviceModel = ServiceModelSectionGroup.GetSectionGroup(config);
var services = serviceModel.Services;
var address = services.Services[serviceName].Endpoints[0].Address;
var addressParts = address.AbsoluteUri.Split('/');
var newAddress = string.Join("/", addressParts.Take(addressParts.Length - 1));
newAddress = newAddress + "/" + Guid.NewGuid().ToString();
services.Services[0].Endpoints[0].Address = new System.Uri(newAddress);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(serviceModel.Name);
var callRouter = new CallRouter();
var sut = new Callmanager(callRouter);
where my class has a constructor:
public CallManager(ICallRouter callRouter)
{
if (callRouter == null)
throw new ArgumentNullException(nameof(callRouter));
this.callRouter = callRouter;
this.callRouter.StartCallHandler += CallStarted;
this.callRouter.EndCallHandler += CallEnded;
hostCallManager = new System.ServiceModel.ServiceHost(callRouter);
hostCallManager.Open();
}
and in my app.config I have:
<system.serviceModel>
<services>
<service name="CallRouter">
<endpoint address="net.tcp://localhost:8011/Service" binding="netTcpBinding" contract="ICallServiceRouter" />
</service>
</services>
So what I am attempting is to generate a new endpoint for each unit test on the fly (by changing the address to include a guid), but this does not seem to work, as each CallRouter instance generated uses the same address when I run all the unit tests in the same test run (the address is different each time I run, so the amendment is working) as if the last amendment of the app.config is being used.
This makes sense as they are all running from the same process, and running in parallel, so the file writes are happening, and due to bad luck the config is modified a number of times before it is ever loaded, but then when it is, all instances of CallRouter get the same values for the endpoint.
Is there any way to make the config that is used by the System.ServiceModel constructor differ for each unit test within the same session??
Aucun commentaire:
Enregistrer un commentaire