小编boa*_*des的帖子

Unity RegisterType与LifetimeManager的奇怪行为

我一直在使用Unity容器,并注意到一个奇怪的行为.我有一个类,它实现了多个接口.我希望这个类可以在不同生命周期的应用程序中的不同位置使用.所以我将IFooDerived1映射到Foo作为Singleton,将IFooDerived2映射到Foo作为Transient.但任何Foo的注册都会破坏以前的Foo注册.

样品:

    interface IFoo { }
    interface IFooDerived1 { }
    interface IFooDerived2 { }
    class Foo : IFoo, IFooDerived1, IFooDerived2 { }

    static void Main(string[] args)
    {
        var container = new UnityContainer();

        container.RegisterType(typeof(IFoo), typeof(Foo), new ExternallyControlledLifetimeManager());
        container.RegisterType(typeof(IFooDerived1), typeof(Foo), new ContainerControlledLifetimeManager());
        container.RegisterType(typeof(IFooDerived2), typeof(Foo), new TransientLifetimeManager());

        foreach(var r in container.Registrations)
        {
            Console.WriteLine("{0} -> {1} : {2}", r.RegisteredType.Name, r.MappedToType.Name, r.LifetimeManagerType.Name);
        }
    }
Run Code Online (Sandbox Code Playgroud)

输出:

    IFoo -> Foo : TransientLifetimeManager
    IFooDerived1 -> Foo : TransientLifetimeManager
    IFooDerived2 -> Foo : TransientLifetimeManager
Run Code Online (Sandbox Code Playgroud)

这是正确的行为吗?有人能给出一些合乎逻辑的解释吗?我可以轻松地使用其他一些方法,我只想了解为什么会发生这种情况.谢谢.

c# inversion-of-control unity-container

5
推荐指数
1
解决办法
306
查看次数

将Web服务配置转换为代码

我在控制台应用程序中有一个SOAP服务客户端,我需要将服务客户端移动到sharepoint 2010.我不想做配置文件部署和其他与sharepoint相关的东西,所以我决定硬编码绑定信息,唯一可配置的选项是URL.但是我在这方面遇到了一些麻烦.我有一个配置:

    <system.serviceModel>
<bindings>
  <customBinding>
    <binding name="SI_PMProjectMaintain_SOUTBinding">
      <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
        messageVersion="Soap11" writeEncoding="utf-8">
        <readerQuotas maxDepth="10000000" maxStringContentLength="10000000"
          maxArrayLength="67108864" maxBytesPerRead="65536" maxNameTableCharCount="100000" />
      </textMessageEncoding>
      <httpTransport authenticationScheme="Basic" bypassProxyOnLocal="false"
        hostNameComparisonMode="StrongWildcard" keepAliveEnabled="false"
        proxyAuthenticationScheme="Basic" realm="XISOAPApps" useDefaultWebProxy="true" />
    </binding>
  </customBinding>
</bindings>
<client>
  <endpoint address="http://url/XISOAPAdapter/MessageServlet?senderParty=&amp;senderService=Param1&amp;receiverParty=&amp;receiverService=&amp;interface=interface&amp;interfaceNamespace=url"
    binding="customBinding" bindingConfiguration="SI_PMProjectMaintain_SOUTBinding"
    contract="PmProjectMaintain.SI_PMProjectMaintain_SOUT" name="HTTP_Port" />
</client>
Run Code Online (Sandbox Code Playgroud)

另外,我有一个代码:

    var service = new ServiceClient();
    service.ClientCredentials.Windows.ClientCredential = new NetworkCredential("user", "password");
    service.ClientCredentials.UserName.UserName = "user";
    service.ClientCredentials.UserName.Password = "password";

    var resp = service.Operation();
    Console.WriteLine(resp.Response);
Run Code Online (Sandbox Code Playgroud)

它按预期工作.现在我想摆脱xml配置并完全将其移动到代码:

public class CustomHttpTransportBinding : CustomBinding
{
    public CustomHttpTransportBinding()
    {
    }

    public override BindingElementCollection CreateBindingElements()
    {
        var …
Run Code Online (Sandbox Code Playgroud)

c# xml wcf web-services .net-3.5

3
推荐指数
1
解决办法
1644
查看次数