【问题标题】:Problem integrating WCF with Sharp architecture将 WCF 与 Sharp 架构集成的问题
【发布时间】:2009-12-02 23:54:07
【问题描述】:

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

[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).

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

我尝试遵循 Northwind 示例,但它不起作用,我会遗漏什么?

【问题讨论】:

  • 对不起,我已经标记了有用的答案。

标签: wcf nhibernate exception s#arp-architecture


【解决方案1】:

我终于找到了答案,我在 ComponentRegistrar 中遗漏了以下行:

container.AddComponent("facilityWcfService", typeof(FacilitiesWcfService));

【讨论】:

  • 仅对于遇到此问题的其他任何人,即使他们已经在容器中注册了他们的组件(如我的情况),单步执行您在容器中注册的代码并深入了解容器.Compnents,找到您注册的组件,并深入了解其 Status 属性以获取有关 IT 可能具有无法解决的依赖关系的消息。
  • 嗨,我知道这是一个非常古老的问答,但我正在考虑为我的 SharpArch MVC 应用程序创建 WCF 服务。我找不到任何像样的样本、指南或文档。你不会碰巧还有一个小示例项目吗?
  • 看北风的例子。容器的东西在哪里。我大致了解了注入对于 asp.net mvc 项目的工作原理。谢谢!
【解决方案2】:

您的服务方法没有返回任何内容,因此需要将其标记为 IsOneWay=true:

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

}

默认情况下,WCF 期望请求/响应,即它期望从服务方法返回响应。 "void" 不算作响应 - 所以只需用 IsOneWay=true 标记那些不返回任何内容的服务方法就可以了。

【讨论】:

  • 虽然这并没有解决问题,但很高兴知道。谢谢
  • 感觉像是 WCF 应该能够自己解决的问题。如果它读取一个属性,为什么不读取返回值呢?无论如何,它可能只在启动时完成一次。
  • @Kristoffer:是的,我同意 - 当返回值为“void”时,WCF 似乎可以默认为 IsOneWay ......哦,好吧......
猜你喜欢
  • 1970-01-01
  • 2023-02-08
  • 2012-06-14
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
  • 2019-10-24
相关资源
最近更新 更多