2

I have a very strange problem. In my MVC 4 application I have this code to initialize StructureMap :

public static class IoC {
public static IContainer Initialize() {
    ObjectFactory.Initialize(x =>
                {
                    x.Scan(scan =>
                            {
                                scan.TheCallingAssembly();
                                scan.WithDefaultConventions();
                            });
                    x.For<IRestHttpClient>().Use<AtlamHttpClient>().Ctor<string>().Is(Settings.AtlamServicesURL);
                });
    ObjectFactory.AssertConfigurationIsValid();
    return ObjectFactory.Container;
}

}

that works as expected. However, I also have a .NET 4.5 Web forms application with the same basic initialization code:

public static class IoC
{
    public static IContainer Configure()
    {
        ObjectFactory.Initialize(x =>
            {
                x.Scan(scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                    scan.AssemblyContainingType<IRestHttpClient>();
                    scan.AssemblyContainingType<MessagePackMediaTypeFormatter>();
                });
                x.For<IRestHttpClient>().Use<AtlamHttpClient>().Ctor<string>().Is(Settings.BaseServiceUrl);
                /*x.SetAllProperties(y =>
                    {
                        y.OfType<IRestHttpClient>();
                    });*/
            });
        ObjectFactory.AssertConfigurationIsValid();
        return ObjectFactory.Container;
    }
}

that throws an exception on AssertConfigurationIsValid() and is failing here in the AtlamHttpClient:

public static List<ContentNegotiator> extensionMappings = new List<ContentNegotiator>()
        {
            new ContentNegotiator("xml", "application/xml", new XmlMediaTypeFormatter()),
            new ContentNegotiator("json", "application/json", new JsonMediaTypeFormatter()),
            new ContentNegotiator("pack", "application/x-msgpack", new MessagePackMediaTypeFormatter())
        };

which in turn calls:

public MessagePackMediaTypeFormatter()
        {
            MediaTypeHeaderValue val = MediaTypeHeaderValue.Parse(mime);
            SupportedMediaTypes.Add(val);
        }

and fails with an ArrayTypeMismatchException. Can't figure out why the first project works fine and the second one is failing. Any help would be greatly appreciated.

4

1 回答 1

1

终于想通了。原来问题是我有一个绑定重定向到 System.Net.Http 2.0 版。删除它,一切正常。

于 2012-09-21T19:02:50.773 回答