• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

kenny2008

  • 管理

View Post

Understand WCF in Silverligt

using System;

namespace Contact.Service
{
public class PersonService : Contact.Service.IPersonService
{
//- @GetPersonData -//
public Person GetPersonData(String personGuid)
{
return new Person
{
FirstName
= "John",
LastName
= "Doe",
City
= "Unknown",
Guid
= personGuid,
PostalCode
= "66062",
State
= "KS"
};
}
}
}
 
1. WCF service are setup using 3 steps:
  • First, Creat a service contract with one or more operation contracts
  • Second, create a service implmentation for those contracts
  • Third, configure a service host to provide that implementation with an endpoint for theat specific contract.
service contract = NET interface + System.ServiceModel.OperationContractAttribute
                   (System.ServiceModel.OperationContractAttribute)
  •  per service contract: 3 to 7 operations contracts
  • The Namespace property set on the attribute specified the namespace used to logically organize services. SOAP services use namespaces to separate various actions.The name space may be arbitrarily chosen, but the client and service must just agree on this namespace.
  • DataContract: clasees System.Runtime.Serualization.DataContractAttribute applied
  • data members: System.Runtime.Serialization.DataMemberAttribute applied to them
  • using datacontract over serializable
 
using System;
using System.ServiceModel;
namespace Contact.Service
{
[ServiceContract(Namespace
=Information.Namespace.Contact)]
public interface IPersonService
{
//- GetPersonData -//
[OperationContract]
Person GetPersonData(String personGuid);
}
}
 Step3: configure a service host. After setup a new web site, we create a Person.svc file.
          Person.svc: <%@ ServiceHost Service="Contact.Service.PersonService" %>
    web.config:
  <?xml version="1.0" encoding="UTF-8"?>
  <configuration>
    <system.serviceModel>
      <services>
        <service name="Contact.Service.PersonService">
          <endpoint address="" binding="basicHttpBinding"
                contract="Contact.Service.IPersonService" />
        </service>
      </services>
    </system.serviceModel>
  </configuration>
  This states that there can be "basicHttpBinding" communication through Contact.Service.IPersonService at address Person.svc to Contact.Service.PersonService
 
The specified address: a relative address. This means that the value of this attribute is appended  onto the base address. In this case the base address is the address specified by our web server. In hte case of a service outside of a web server, then you can sepcify an absolute address here.Our service is at Person.svc, thus we have already provided for us the base URL.
 
the binding specifies how the information is to be format for transfer. There's actually nothing too magical about a binding, though. It's really just a collection of binding elements and pre-configured parameter defaults, which are easily changed in configuration. Each binding element will have at a minimum two binding elements.
 
the contract: client are communication through a contract to the hosted service
 
version contracts: Perhaps you added or removed a parameter from your contract. Unless you want to break all the clients accessing the service, you must keep the old contract applied to your service (read: keep the old interface on the service class) and keep the old endpoint running by setting up a parallel endpoint.You will add a new service endpoint every time you change your version, change your contract, or change your binding.
 
Service Access Without Magices
  
 We've seen how we configure a WCF host by creating an endpoint specifying an address, binding and contract. As it turns out, this is all that's required on the client side as well. For both .NET and Silverlight, you merge an address and a binding with a contract in a channel factory to create a channel.
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
 EndpointAddress endpointAddress = new EndpointAddress("http://localhost:1003/Person.svc");
IPersonService personService = new ChannelFactory<IPersonService>(basicHttpBinding, endpointAddress).CreateChannel();
 Person person = personService.GetPersonData("F488D20B-FC27-4631-9FB9-83AF616AB5A6");
 
In this you have an address (the EndpointAddress object), a binding (BasicHttpBinding), and a contract (IPersonService) meeting in System.ServiceModel.ChannelFactory<TServiceContract> to create a channel which implements the IPersonService interface (and others).
 
web.config
<system.serviceModel>
  <bindings>
    <customBinding>
      <binding name="HttpTextCustomBinding">
        <textMessageEncoding messageVersion="Soap11" />
        <httpTransport/>
      </binding>
    </customBinding>
  </bindings>
   <client>
    <endpoint  address="http://localhost:1003/Person.svc" binding="customBinding"
        contract="Contact.Service.IPersonService" />
   </client>
</system.serviceModel>
 
The Async Pattern: which states that asynchronous methods are to follow the following pattern:
 
Dispatcher.BeginInvoke:
    You must do this if you are on a visual element and want to access visual elements in your callback. This will grant you access to the UI thread.
private void Write(String text)
{
  this.Dispatcher.BeginInvoke(delegate
  {
    spMain.Children.Add(new TextBlock { Text = text });
  });
}
 
External Web Site Access
By default, you cannot use Silverlight to access a web site which differs by domain, IP address, or port number.
ClientAccessPolicy.xml
<?xml version="1.0" encoding="utf-8"?>
  <access-policy>
    <cross-domain-access>
      <policy>
        <allow-from>
          <domain uri="*"/>
        </allow-from>
        <grant-to>
          <resource path="/" include-subpaths="true"/>
        </grant-to>
      </policy>
    </cross-domain-access>
  </access-policy>
 
Because for WCF in Silverlight to call a service outside of the local web site, you must also specify what headers to allow. In our case, we want to allow the SOAPAction header. The SOAPAction header is what a SOAP service uses to specify what action to run on the service.
<?xml version="1.0" encoding="utf-8"?>
  <access-policy>
    <cross-domain-access>
      <policy>
        <allow-from http-request-headers="SOAPAction">
          <domain uri="*"/>
        </allow-from>
        <grant-to>
          <resource path="/" include-subpaths="true"/>
        </grant-to>
      </policy>
    </cross-domain-access>
  </access-policy>
   

posted on 2010-11-13 14:02  kenny2008  阅读(194)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3