Spring.NET 1.3.1 新特性探索系列2——WCF命名空間解析器

來源:互聯網
上載者:User

  

  Spring.NET對分布式程式的支援是有目共睹的。在1.3.1之前的版本,對Remoting和Webservice支援的很好,並且有其對應的解析器,但對WCF支援的不是很完美。然而1.3.1版本加入了WCF的命名空間解析器功能。我們匯入 xmlns:wcf="http://www.springframework.net/wcf“  命名空間後,便可以使用解析器提供的配置了。

 

  一、建立一個WCF契約程式集:WcfContract。

  建立介面ISpringContract

  

namespace WcfContract
{
    [ServiceContract]
    public interface ISpringContract
    {
        [OperationContract]
        string GetData(int value);
    }
}

 

 

 

  二、建立WCF的伺服器端項目WcfServer。

  建立SpringService.svc檔案,其SpringService類繼承ISpringContract

  

SpringService

namespace WcfServer
{
    public class SpringService : ISpringContract
    {
        public string GetData(int value)
        {
            return string.Format("你輸入的是: {0}", value);
        }

      
    }
}

 

   三、建立WCF用戶端項目WcfClient

  配置app.config

  

app.config

<?xml version="1.0"?>
<configuration>
  
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
    </sectionGroup>
  </configSections>

  <spring>
    <context>
      <resource uri="assembly://WcfClient/WcfClient.Config/Wcf.xml"/>
    </context>
  </spring>
  
  <system.serviceModel>
        <bindings>
              <basicHttpBinding>
                    <binding name="BasicHttpBinding_ISpringContract" closeTimeout="00:01:00"
                          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                          useDefaultWebProxy="true">
                          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                                maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                          <security mode="None">
                                <transport clientCredentialType="None" proxyCredentialType="None"
                                      realm="" />
                                <message clientCredentialType="UserName" algorithmSuite="Default" />
                          </security>
                    </binding>
              </basicHttpBinding>
        </bindings>
        <client>
              <endpoint address="http://localhost:50784/SpringService.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISpringContract"
                    contract="WcfContract.ISpringContract" name="BasicHttpBinding_ISpringContract" />
        </client>
    </system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

 

 

 四、在WCF用戶端增加設定檔Wcf.xml

  

Wcf.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
         xmlns:wcf="http://www.springframework.net/wcf">

  <wcf:channelFactory id="SpringProxy"
    channelType="WcfContract.ISpringContract,WcfContract"              
    endpointConfigurationName="BasicHttpBinding_ISpringContract">
    <!--身分識別驗證-->
    <wcf:property name="Credentials.Windows.ClientCredential" value="Domain\Login:Password" />
  </wcf:channelFactory>
 
</objects>

 

 

  這樣,我可以無需“加入服務參考”,來實現動態調用WCF。注意的是我們需要引用WcfContract項目。

 

  五、WCF用戶端調用部分代碼

  

Program

class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            WcfContract.ISpringContract proxy = ctx.GetObject("SpringProxy") as WcfContract.ISpringContract;
            var msg = proxy.GetData(520);
            Console.Write(msg);

            Console.ReadLine();
        }
    }

 

 

   運行效果:

  

 

 

  出處:http://www.cnblogs.com/GoodHelper/archive/2010/12/22/SpringNet131Wcf.html

  歡迎轉載,但需保留著作權。

  代碼下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.