English 中文(简体)
WCF 合同误配错误,使用自动登记系统通过海峡不动产登记终点
原标题:WCF contract mismatch error using Autofac to register endpoint via ChannelFactory

我有一个以简单MVC申请取用的WCF服务。

当我试图从一个不同的MVC中发出同样终点的电线时,与Autofac连接起来。 我收到约束/合同不匹配的例外。 类似:

Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:6985/ProductService.svc. The client and service bindings may be mismatched.

System.Net.WebException: The remote server returned an error: (415) Unsupported Media Type.

I m reasonably confident I do not have a mismatch in the configuration settings on either end, I base this confidence on testing the exact same settings on a WCF + MVC combination where Autofac is not present. The config settings are on pastebin.com/t7wfR77h.

因此,如果我与自治州登记扶养/最后任命的方式是问题的话,我会有所帮助。

*Application_Start* code in MVC app for Autofac setup:

var builder = new ContainerBuilder();
//other registrations...

builder.Register(c => 
            new ChannelFactory<IProductService>(
                new WSHttpBinding("ProductService_wsHttpBinding"),
                new EndpointAddress("http://localhost:6985/ProductService.svc")
            )
        ).SingleInstance();

builder.Register(c =>
        {
            var factory = c.Resolve<ChannelFactory<IProductService>>();
            return factory.CreateChannel();
        }
      ).InstancePerHttpRequest();

var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

(完整性)在我使用这一工具时,只需要注入1个依赖性的产品主计长,非常简单:

public class ProductController : AsyncController
{
    private IProductService _service;

    public ProductController(IProductService ps)
    {
        _service = ps;
    }

    //...
    //later simply call
    _service.SomeMethod();
}
问题回答

正如对@Nick Josevski的评论中提到的,我能够找到类似于工作的东西。

在我的MVC3申请中,申请 起始方法,我有以下法典:

protected void Application_Start()
{
    var builder = new ContainerBuilder();
    builder.Register(c => new ChannelFactory<ICartService>("CartService")).SingleInstance();
    builder.Register(c => c.Resolve<ChannelFactory<ICartService>>().CreateChannel()).InstancePerHttpRequest();
    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

    // other MVC startup activities, like registering areas and routes
}

这些登记从网上收集了WCF配置数据。 我也加快登记工作,以达到法典规定的终点。 简言之,这里有一部分相关的用户-边网络。

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="BasicHttpBinding" ... />
    </basicHttpBinding>
  </bindings>
  <client>
    <endpoint address="http://localhost:50930/Purchasing/CartService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding"
        contract="CartService.ICartService" name="CartService" />
  </client>
</system.serviceModel>

接着,在我的控制人员中,我有以下守则:

using Autofac.Features.OwnedInstances;

public class BulkCartController : Controller
{
    private readonly Owned<ICartService> cartService_;

    public BulkCartController(Owned<ICartService> cartService)
    {
        cartService_ = cartService;
    }

    protected override void Dispose(bool disposing) // defined in Controller
    {
        cartService_.Dispose();
        base.Dispose(disposing);
    }

    //
    // GET: /BulkCart/Get/1
    public ActionResult Get(int id)
    {
        var model = new ShoppingCart { ShoppingCartId = id };
        using (var cartService = cartService_)
        {
            model.Items = cartService.Value.GetCartProductItems(id);
        }
        return View("Get", model);
    }
}

单位测试就是如此:

using Autofac.Features.OwnedInstances;
using Autofac.Util;
using Moq;

[TestMethod]
public void Get_ReturnsItemsInTheGivenCart()
{
    var mock = new Mock<ICartService>(MockBehavior.Strict);
    mock.Setup(x => x.GetCartProductItems(2)).Returns(new CartProductItemViewObject[0]);
    var controller = new BulkCartController(new Owned<ICartService>(mock.Object, new Autofac.Util.Disposable())); 
    var result = controller.Get(2);

    Assert.IsInstanceOfType(result, typeof(ViewResult));
    var view = (ViewResult)result;
    Assert.AreEqual("Get", view.ViewName);

    Assert.IsInstanceOfType(view.ViewData.Model, typeof(ShoppingCart));
    var model = (ShoppingCart)view.ViewData.Model;
    Assert.AreEqual(2, model.ShoppingCartId);
    Assert.AreEqual(0, model.Items.Length);
}

我通过抽象的控制器测试基类界定的单位测试验证处置:

[TestClass]
public abstract class ControllerWithServiceTestBase<TController, TService>
    where TController : Controller
    where TService : class
{
    [TestMethod]
    public virtual void Dispose_DisposesTheService()
    {
        var disposable = new Mock<IDisposable>(MockBehavior.Strict);
        disposable.Setup(x => x.Dispose()).Verifiable();

        var controller = (TController) Activator.CreateInstance(typeof(TController), new Owned<TService>(null, disposable.Object));
        controller.Dispose();

        disposable.Verify();
    }
}

我尚未知道的一件事是使用<代码>。 rel=“nofollow”





相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签