Skip to content

Instantly share code, notes, and snippets.

@k0stya
Last active December 12, 2015 10:39
Show Gist options
  • Save k0stya/4760685 to your computer and use it in GitHub Desktop.
Save k0stya/4760685 to your computer and use it in GitHub Desktop.
Helper class that allows to do integration testing of WCF services using AspNetDevelopmentServerAttribute.
// Example of usage
[TestMethod]
[AspNetDevelopmentServer("WcfService", "Physical path to your WCF project")]
public void TestMethod1()
{
ServiceClient target = new ServiceClient();
Assert.IsTrue(WcfWebServiceHelper.TryUrlRedirection(target, TestContext, "WcfService"));
int value = 0;
string expected = "You entered: 0";
string actual;
actual = target.GetData(value);
Assert.AreEqual(expected, actual);
}
public class WcfWebServiceHelper
{
public static bool TryUrlRedirection(object client, TestContext context, string identifier)
{
bool result = true;
try
{
PropertyInfo property = client.GetType().GetProperty("Endpoint");
string webServer = context.Properties[
string.Format("AspNetDevelopmentServer.{0}", identifier)].ToString();
Uri webServerUri = new Uri(webServer);
ServiceEndpoint endpoint = (ServiceEndpoint)property.GetValue(client, null);
EndpointAddressBuilder builder = new EndpointAddressBuilder(endpoint.Address);
builder.Uri = new Uri(
endpoint.Address.Uri.OriginalString.Replace(
endpoint.Address.Uri.Authority, webServerUri.Authority));
endpoint.Address = builder.ToEndpointAddress();
}
catch (Exception e)
{
context.WriteLine(e.Message);
result = false;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment