• WCF With StructureMap


    渐渐用惯了Structure,却发现不能直接支持Wcf。

    就着手查查资料:http://lostechies.com/jimmybogard/2008/07/30/integrating-structuremap-with-wcf/

    上面的大神不喜欢XML,直接无视XML方式,我倒。

    看来能自己动手:

     创建 StructureMapServiceBehavior

     
        public class StructureMapServiceBehavior : IServiceBehavior
        {
            public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
                foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
                {
                    ChannelDispatcher cd = cdb as ChannelDispatcher;
                    if (cd != null)
                    {
                        foreach (EndpointDispatcher ed in cd.Endpoints)
                        {
                            ed.DispatchRuntime.InstanceProvider =
                                new StructureMapInstanceProvider(serviceDescription.ServiceType);
                        }
                    }
                }
            }
    
            public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
                Collection endpoints, BindingParameterCollection bindingParameters)
            {
            }
    
            public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
            {
            }
        }
    
    

      创建 StructureMapInstanceProvider

        public class StructureMapInstanceProvider : IInstanceProvider
        {
            private readonly Type _serviceType;
    
            public StructureMapInstanceProvider(Type serviceType)
            {
                _serviceType = serviceType;
            }
    
            public object GetInstance(InstanceContext instanceContext)
            {
                return GetInstance(instanceContext, null);
            }
    
            public object GetInstance(InstanceContext instanceContext, Message message)
            {
                var obj = ObjectFactory.GetInstance(_serviceType);
                //Console.WriteLine(obj.GetHashCode());
                return obj;
            }
    
            public void ReleaseInstance(InstanceContext instanceContext, object instance)
            {
            }
        }
    
    

      创建 StructureMapBehaviorExtension

        public class StructureMapBehaviorExtension : BehaviorExtensionElement
        {
    
            public override Type BehaviorType
            {
                get { return typeof(StructureMapServiceBehavior); }
            }
    
            protected override object CreateBehavior()
            {
                return new StructureMapServiceBehavior();
            }
        }
    

      然后设置app.config

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
     
      <system.web>
        <compilation debug="true" />
      </system.web>
      <!-- 部署服务库项目时,必须将配置文件的内容添加到 
      主机的 app.config 文件中。System.Configuration 不支持库的配置文件。-->
      <system.serviceModel>
        <services>
          <service name="WcfStructureMap3.Service1">
            <host>
              <baseAddresses>
                <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfStructureMap3/Service1/" />
              </baseAddresses>
            </host>
            <!-- Service Endpoints -->
            <!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
            <endpoint address="" binding="basicHttpBinding" contract="WcfStructureMap3.IService1">
              <!-- 
                  部署时,应删除或替换下列标识元素,以反映
                 用来运行所部署服务的标识。删除之后,WCF 将
                  自动推断相应标识。
              -->
              <identity>
                <dns value="localhost"/>
              </identity>
            </endpoint>
            <!-- Metadata Endpoints -->
            <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 --> 
            <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- 为避免泄漏元数据信息,
              请在部署前将以下值设置为 false -->
              <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
              <!-- 要接收故障异常详细信息以进行调试,
              请将以下值设置为 true。在部署前设置为 false 
                以避免泄漏异常信息-->
              <serviceDebug includeExceptionDetailInFaults="False" />
               
              <StructureMapExt />
               
            </behavior>
          </serviceBehaviors>
        </behaviors>
     
        <extensions>
          <behaviorExtensions>
            <add name="StructureMapExt"
                 type="WcfStructureMap3.StructureMapBehaviorExtension, WcfStructureMap3"/>
          </behaviorExtensions>
        </extensions>
     
      </system.serviceModel>
     
    </configuration>

       简单写一个宿主程序

     
    class Program
        {
            static void Main(string[] args)
            {
                ObjectFactory.Initialize( inti =>
                {
                    inti.For()Use();
                }
                );
    
                ServiceHost host = new ServiceHost(typeof(WcfStructureMap3.Service1));
                host.Open();
    
                Console.WriteLine("服务启动");
                
                Console.ReadLine();
            }
        }
    
  • 相关阅读:
    关于数据库 长度和小数点的关系和坑
    温故而知新,jquery选择器$=
    局域网访问本地localhost页面
    解决谷歌浏览器和360浏览器 input 自动填充淡黄色背景色的问题
    抢月饼 浏览器插件开发
    css 多行溢出
    ss服务待研究
    NUnit笔记
    5分钟实现VS2010整合NUnit进行单元测试
    vs2010中使用Nunit测试c#代码结果的正确性
  • 原文地址:https://www.cnblogs.com/bikaqiou2000/p/3565639.html
Copyright © 2020-2023  润新知