我与使用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除外)。

我试图按照罗斯文的例子,但它不工作,我能会丢失?

有帮助吗?

解决方案

最后,我找到了答案,我错过了在ComponentRegistrar以下行:

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

其他提示

你不返回从您的服务方法什么,所以需要将其标记为IsOneWay =真:

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

}

默认情况下,WCF预计请求/响应,即,其期望得到一个响应从服务方法回来。 “作废”不能算作回应 - 所以只是纪念那些服务的方法不与IsOneWay=true返回任何东西,你应该罚款。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top