代码之家  ›  专栏  ›  技术社区  ›  Leg10n

将WCF与Sharp体系结构集成的问题

  •  4
  • Leg10n  · 技术社区  · 16 年前

    我正在使用一个使用wcf和sharp架构的应用程序,我正在尝试创建一个服务来写入数据库。这是我的服务:

    [ServiceContract]
    public interface IFacilitiesWcfService : ICloseableAndAbortable
    {
        [OperationContract]
        void AddFacility(string facility);
    
    }
    
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    class FacilitiesWcfService:IFacilitiesWcfService
    {
        public FacilitiesWcfService(IRepositoryWithTypedId<Facility,string> facilityRepository)
        {
            Check.Require(facilityRepository != null, "facilityRepository may not be null");
    
            this.facilityRepository = facilityRepository;
        }
        private readonly IRepositoryWithTypedId<Facility,string> facilityRepository;
    
        public void AddFacility(string facility)
        {
            facilityRepository.DbContext.BeginTransaction();
    
            Facility newFacility = new Facility();
            newFacility.SetAssignedIdTo(facility);
            newFacility.NAME=facility;
            newFacility.ADDRESS = facility;
    
            facilityRepository.DbContext.CommitTransaction();
        }
        public void Abort() { }
    
        public void Close() { }
    }
    

    以及web项目中的LogisticsWCF.svc文件:

    <%@ ServiceHost Language="C#" Debug="true" Service="Project.Wcf.FacilitiesWcfService"
     Factory="SharpArch.Wcf.NHibernate.ServiceHostFactory, SharpArch.Wcf" %>
    

    svcutil.exe http://localhost:1905/LogisticsWCF.svc?wsdl

    [TestFixture]
    class WCFLogisticsTests
    {
        [Test]
        public void CanAddFacility()
        {
    
            FacilitiesWcfServiceClient facility = new FacilitiesWcfServiceClient();
            facility.AddFacility("NEW");
            facility.Close();
        }
    }
    

    TestCase 'Tests.Project.Web.WCFLogisticsTests.CanAddFacility'
    failed: System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail] : The needed dependency of type FacilitiesWcfService could not be located with the ServiceLocator. You'll need to register it with the Common Service Locator (CSL) via your IoC's CSL adapter.
    
        Server stack trace:
        at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
        at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
        at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
        at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
        at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
        at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
    
        Exception rethrown at [0]:
        at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
        at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
        at IFacilitiesWcfService.AddFacility(String facility)
        C:\Documents and Settings\epena\My Documents\SVN\Project\tests\Project.Tests\FacilitiesWcfService.cs(58,0): at FacilitiesWcfServiceClient.AddFacility(String facility)
        WCFLogisticsTests.cs(18,0): at Tests.Project.Web.WCFLogisticsTests.CanAddFacility()
    
    
    0 passed, 1 failed, 0 skipped, took 4.52 seconds (NUnit 2.5.2).
    

    我想我缺少了一些夏普架构的配置,因为当我不使用 Factory="SharpArch.Wcf.NHibernate.ServiceHostFactory, SharpArch.Wcf" 在.svc文件中,我没有得到异常,但我无法向数据库写入任何内容(我得到一个ISession not configured异常)。

    我试着模仿北风的例子,但它不起作用,我能错过什么?

    2 回复  |  直到 16 年前
        1
  •  3
  •   Leg10n    16 年前

    最后,我找到了答案,我在ComponentRegister中缺少以下行:

    container.AddComponent("facilityWcfService", typeof(FacilitiesWcfService));
    
        2
  •  1
  •   marc_s MisterSmith    16 年前

    [ServiceContract]
    public interface IFacilitiesWcfService : ICloseableAndAbortable
    {
        [OperationContract(IsOneWay=true)]
        void AddFacility(string facility);
    
    }
    

    默认情况下,WCF需要请求/响应,即它需要从服务方法返回响应。”void”不算作响应,所以只需标记那个些不返回任何带有 IsOneWay=true

    推荐文章