【发布时间】:2012-02-12 16:54:03
【问题描述】:
有没有办法做到这一点...
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
...以编程方式?
原因是我想在集成测试我的服务时将我的服务实例直接传递到我的自托管帮助器类中。
我正在使用 Castle Windsor 创建我的所有对象,在使用测试网站时效果很好。但是当我尝试使用我的 HttpWebService 帮助程序类时出现以下错误...
System.InvalidOperationException was unhandled by user code
Message=In order to use one of the ServiceHost constructors that takes a service instance, the InstanceContextMode of the service must be set to InstanceContextMode.Single. This can be configured via the ServiceBehaviorAttribute. Otherwise, please consider using the ServiceHost constructors that take a Type argument.
Source=System.ServiceModel
这是我的助手类的构造函数...
public HttpWebService(string baseUri, string acceptType, TApi serviceInstance = null)
{
_baseUri = baseUri;
_acceptType = acceptType.ToLower();
_host = serviceInstance == null
? new HttpServiceHost(typeof (TApi), baseUri)
: new HttpServiceHost(serviceInstance, baseUri);
_host.Open();
_client = new HttpClient();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_acceptType));
}
所以,我需要在“集成测试模式”下以编程方式设置InstanceContextMode,即在我的助手类中。
我想我需要做这样的事情......
if (serviceInstance != null)
{
_host = new HttpServiceHost(serviceInstance, baseUri);
var whatDoIDoNow = null;
_host.Description.Behaviors.Add(whatDoIDoNow);
}
任何帮助/指导都会很棒,因为我真的坚持这一点。
【问题讨论】:
标签: wcf-web-api