问题描述
我正在尝试对 WCF 应用程序进行单元测试。 这是我尝试过的:
public class Service1 : IService1
{
public string GetData(int value)
{
string val = ConfigurationManager.AppSettings["mykey"].ToString();
return string.Format("You entered: {0}. mykey={1}",value,val);
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
ServiceHost serviceHost = null;
try
{
string url = "http://localhost:56666/Serivce1";
var baseAddress = new Uri(url);
serviceHost = new ServiceHost(typeof(Service1),baseAddress);
Binding binding = new WSHttpBinding(SecurityMode.None);
serviceHost.AddServiceEndpoint(typeof(IService1),binding,"Service1");
var smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(smb);
serviceHost.open(); // if this fails,you have to run Visual Studio as admin
BasicHttpBinding myBinding = new BasicHttpBinding();
ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(myBinding);
EndpointAddress myEndpoint = new EndpointAddress(url);
IService1 wcfClient = myChannelFactory.CreateChannel(myEndpoint);
string s = wcfClient.GetData(39);
serviceHost.Close();
}
finally
{
if (serviceHost != null)
{
((Idisposable)serviceHost).dispose();
}
}
}
}
}
当我调用 wcfClient.GetData(39); 我收到此错误: System.ServiceModel.EndpointNotFoundException:在 http://localhost:56666/Serivce1 上没有可以接受消息的端点侦听。这通常是由不正确的地址或 SOAP 操作引起的。有关更多详细信息,请参阅 InnerException(如果存在)。 ---> System.Net.WebException:远程服务器返回错误:(404) 未找到。
知道为什么会出现此错误以及如何使其正常工作吗?
解决方法
我让它工作了,但使绑定保持一致并修复了与 WCF 配置相关的问题,但没有很好的解决方法:
public class Service1 : IService1
{
public string GetData(int value)
{
string val = "Nothing";
if(ConfigurationManager.AppSettings != null && ConfigurationManager.AppSettings["mykey"] != null) // when normally run
{
val = ConfigurationManager.AppSettings["mykey"].ToString();
}
else // when run via unittest:
{
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "web.config";
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap,ConfigurationUserLevel.None);
ConfigurationManager.RefreshSection("appSettings"); // this does not work,so I I wrote the loop:
foreach (KeyValueConfigurationElement kv in configuration.AppSettings.Settings)
{
ConfigurationManager.AppSettings[kv.Key] = kv.Value;
}
if (ConfigurationManager.AppSettings["mykey"] != null)
{
val = ConfigurationManager.AppSettings["mykey"].ToString();
}
}
return string.Format("You entered: {0}. mykey={1}",value,val);
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
ServiceHost serviceHost = null;
try
{
string url = "http://localhost:56669";
var baseAddress = new Uri(url);
serviceHost = new ServiceHost(typeof(Service1),baseAddress);
BasicHttpBinding binding = new BasicHttpBinding();
serviceHost.AddServiceEndpoint(typeof(IService1),binding,"");
var smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(smb);
// enabling server side exception details to help debug:
var behavior = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
behavior.IncludeExceptionDetailInFaults = true;
serviceHost.Open(); // if this fails,you have to run Visual Studio as admin
ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(binding);
EndpointAddress myEndpoint = new EndpointAddress(url);
IService1 wcfClient = myChannelFactory.CreateChannel(myEndpoint);
string result = wcfClient.GetData(39);
Assert.AreEqual(string.Format("You entered: {0}. mykey=myval",39),result);
serviceHost.Close();
}
finally
{
if (serviceHost != null)
{
((IDisposable)serviceHost).Dispose();
}
}
}
}
欢迎任何改进配置加载的建议。