C# 通过Castle WCF集成设施使用PerWcfSession生活方式
以下代码使用Castle Windsor 3.0注册WCF自托管服务:C# 通过Castle WCF集成设施使用PerWcfSession生活方式,c#,wcf,castle-windsor,wcffacility,C#,Wcf,Castle Windsor,Wcffacility,以下代码使用Castle Windsor 3.0注册WCF自托管服务: using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using Castle.Facilities.WcfIntegration; using Castle.MicroKernel.Registration; using Castle.Windsor; namespace SelfHos
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using Castle.Facilities.WcfIntegration;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
namespace SelfHost
{
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string SayHello(string name);
}
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class HelloWorldService : IHelloWorldService
{
private readonly PerSession _perSession;
public HelloWorldService(PerSession perSession)
{
_perSession = perSession;
}
public string SayHello(string name)
{
return string.Format("Hello, {0} {1}", name, _perSession.Info());
}
}
public class PerSession
{
private readonly string _now;
public PerSession()
{
_now = DateTime.Now.ToString();
}
public string Info()
{
return _now;
}
}
internal class Program
{
private static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8080/hello");
var container = new WindsorContainer();
container.AddFacility<WcfFacility>();
container.Register(
Component.For<PerSession>().LifeStyle.PerWcfSession(),
Component.For<IHelloWorldService>()
.ImplementedBy<HelloWorldService>()
.AsWcfService(
new DefaultServiceModel()
.AddBaseAddresses(baseAddress)
.AddEndpoints(WcfEndpoint.BoundTo(new BasicHttpBinding()).At("basic"))
.PublishMetadata(o => o.EnableHttpGet())
)
);
Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.ServiceModel;
使用Castle.Facilities.wcfinintegration;
使用Castle.MicroKernel.Registration;
使用温莎城堡;
命名空间自宿主
{
[服务合同]
公共接口IHelloWorldService
{
[经营合同]
字符串SayHello(字符串名称);
}
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
公共类HelloWorldService:IHelloWorldService
{
私有只读会话_PerSession;
公共HelloWorldService(PerSession)
{
_perSession=perSession;
}
公共字符串SayHello(字符串名称)
{
返回string.Format(“Hello,{0}{1}”,name,_perSession.Info());
}
}
公共阶级迫害
{
私有只读字符串\u现在;
公众迫害()
{
_now=DateTime.now.ToString();
}
公共字符串信息()
{
现在就回来,;
}
}
内部课程计划
{
私有静态void Main(字符串[]args)
{
Uri baseAddress=新Uri(“http://localhost:8080/hello");
var container=新的WindsorContainer();
container.AddFacility();
集装箱。登记(
Component.For().LifeStyle.PerWcfSession(),
用于()的组件
.由()实施
.ASWCF服务(
新的DefaultServiceModel()
.AddBaseAddresses(基本地址)
.AddEndpoints(WcfEndpoint.BoundTo(new BasicHttpBinding()).At(“basic”))
.PublishMetadata(o=>o.EnableHttpGet())
)
);
WriteLine(“服务在{0}处准备就绪”,基地址);
控制台。WriteLine(“按以停止服务”);
Console.ReadLine();
}
}
}
尝试使用WcfTestClient.exe调用SayHello方法会导致以下错误:
无法获取组件SelfHost.PerSession的作用域。这是最重要的
可能是自定义IScopeAccessor中的错误,或者您正在尝试
访问作用域之外的作用域组件(如每个web请求
web请求等之外的组件)
使用PerWcfSession组件的正确方法是什么?因此我遗漏了一些东西: ServiceContract需要设置SessionMode属性
[ServiceContract(SessionMode = SessionMode.Required)]
同样,ServiceBehavior需要设置InstanceContextMode
[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerSession)]
最后,服务注册需要更改默认(单例)的生活方式,以便为每个请求重新创建它(并重新评估依赖关系)-暂时或PerWcfSession可以工作
此外,由于我们需要会话,绑定需要从basicHttpBinding更改为支持会话的绑定:
Component.For<IHelloWorldService>()
.ImplementedBy<HelloWorldService>()
.LifestyleTransient()
.AsWcfService(
new DefaultServiceModel()
.AddBaseAddresses(baseAddress)
.AddEndpoints(WcfEndpoint.BoundTo(new WSHttpBinding()).At("myBinding"))
.PublishMetadata(o => o.EnableHttpGet())
)
Component.For()
.由()实施
.生活方式
.ASWCF服务(
新的DefaultServiceModel()
.AddBaseAddresses(基本地址)
.AddEndpoints(WcfEndpoint.BoundTo(新的WSHttpBinding()).At(“myBinding”))
.PublishMetadata(o=>o.EnableHttpGet())
)
这使得最终的代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using Castle.Facilities.WcfIntegration;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
namespace SelfHost
{
[ServiceContract(SessionMode = SessionMode.Required)]
public interface IHelloWorldService
{
[OperationContract]
string SayHello(string name);
}
[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerSession)]
public class HelloWorldService : IHelloWorldService
{
private readonly PerSession _perSession;
public HelloWorldService(PerSession perSession)
{
_perSession = perSession;
}
public string SayHello(string name)
{
return string.Format("Hello, {0} {1}", name, _perSession.Info());
}
}
public class PerSession
{
private readonly string _now;
public PerSession()
{
_now = DateTime.Now.ToString();
}
public string Info()
{
return _now;
}
}
internal class Program
{
private static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8080/hello");
var container = new WindsorContainer();
container.AddFacility<WcfFacility>();
container.Register(
Component.For<PerSession>().LifeStyle.PerWebRequest,
Component.For<IHelloWorldService>()
.ImplementedBy<HelloWorldService>()
.LifeStyle.PerWebRequest
.AsWcfService(
new DefaultServiceModel()
.AddBaseAddresses(baseAddress)
.AddEndpoints(WcfEndpoint.BoundTo(new WSHttpBinding()).At("myBinding"))
.PublishMetadata(o => o.EnableHttpGet())
)
);
Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.ServiceModel;
使用Castle.Facilities.wcfinintegration;
使用Castle.MicroKernel.Registration;
使用温莎城堡;
命名空间自宿主
{
[ServiceContract(SessionMode=SessionMode.Required)]
公共接口IHelloWorldService
{
[经营合同]
字符串SayHello(字符串名称);
}
[ServiceBehavior(IncludeExceptionDetailInFaults=true,InstanceContextMode=InstanceContextMode.PerSession)]
公共类HelloWorldService:IHelloWorldService
{
私有只读会话_PerSession;
公共HelloWorldService(PerSession)
{
_perSession=perSession;
}
公共字符串SayHello(字符串名称)
{
返回string.Format(“Hello,{0}{1}”,name,_perSession.Info());
}
}
公共阶级迫害
{
私有只读字符串\u现在;
公众迫害()
{
_now=DateTime.now.ToString();
}
公共字符串信息()
{
现在就回来,;
}
}
内部课程计划
{
私有静态void Main(字符串[]args)
{
Uri baseAddress=新Uri(“http://localhost:8080/hello");
var container=新的WindsorContainer();
container.AddFacility();
集装箱。登记(
Component.For().LifeStyle.PerWebRequest,
用于()的组件
.由()实施
.livelope.PerWebRequest
.ASWCF服务(
新的DefaultServiceModel()
.AddBaseAddresses(基本地址)
.AddEndpoints(WcfEndpoint.BoundTo(新的WSHttpBinding()).At(“myBinding”))
.PublishMetadata(o=>o.EnableHttpGet())
)
);
WriteLine(“服务在{0}处准备就绪”,基地址);
控制台。WriteLine(“按以停止服务”);
Console.ReadLine();
}
}
}
您在哪里定义“myBinding”和基址?Cheers MyBinding是在问题的Program类中定义的谢谢David的回答,我想你不能用这个代码更新你的答案吗?我自己在获取绑定到特定地址时遇到问题。干杯,走吧!“mybinding”只是