SilverlightCatchWcfError

SliverLight捕捉WCF服务异常,

1 Contract

[ServiceContract]
    public interface IWCFService
    {
        [OperationContract]
        int  DoWork(int a,int b);

        [OperationContract]
        int DoWork2(int a, int b);

        [OperationContract]
        int DoDevide(int a, int b, ref string errorMessage);

        [OperationContract]
        int Devide(int a, int b, ref Exception error);
    }

 

2 Service

 

public class WCFService : IWCFService
    {
        public int DoDevide(int a, int b, ref string errorMessage)
        {
            if (b == 0)
            {
                errorMessage = "Devided by Zero";
                return 0;
            }
            errorMessage = "11111";
            return a / b;
        }

        public int Devide(int a, int b, ref Exception error)
        {
            try
            {
                error = null;
                return a / b;
            }
            catch (Exception ex)
            {
                error = ex;
                return 0;
            }
        }
    }

 

3 跨域访问策略服务接口

[ServiceContract]
    public interface IDomainService
    {
        [OperationContract]
        [WebGet(UriTemplate = "ClientAccessPolicy.xml")]
        Message ProvidePolicyFile();
    }

 public System.ServiceModel.Channels.Message ProvidePolicyFile()
        {
             FileStream filestream = File.Open(@"ClientAccessPolicy.xml", FileMode.Open);
            // Either specify ClientAcessPolicy.xml file path properly
            // or put that in /Bin folder of the console application
            XmlReader reader = XmlReader.Create(filestream);
            System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None, "", reader);
           return result;
        }

 

5 clientaccesspolicy.xml

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

6  App.Config

<configuration>
    <system.serviceModel>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WCFBase.WCFServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="DomainServiceBehavior">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
     
      <!--
      <bindings>
        <basicHttpBinding>
          <binding name="NoneSecurity"
   maxBufferPoolSize="12000000" maxReceivedMessageSize="12000000" useDefaultWebProxy="false">
            <readerQuotas maxStringContentLength="12000000" maxArrayLength="12000000"/>
            <security mode="None"/>
          </binding>
        </basicHttpBinding>
      </bindings>
      -->
     
        <services>
            <service behaviorConfiguration="WCFBase.WCFServiceBehavior" name="WCFBase.WCFService">
                <endpoint address="" binding="basicHttpBinding" contract="WCFBase.IWCFService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:9090/WCFService/" />
                    </baseAddresses>
                </host>
            </service>
            <service name="WCFBase.DomainService">
                <endpoint address="" behaviorConfiguration="DomainServiceBehavior"
                    binding="webHttpBinding" contract="WCFBase.IDomainService" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:9090/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
     
    </system.serviceModel>
 
</configuration>
7 Host

 static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(WCFService));
            host.Open();

            ServiceHost crossDomainserviceHost = new ServiceHost(typeof(DomainService));
            crossDomainserviceHost.Open();

            Console.WriteLine("I'am Here.");
            Console.ReadKey();
            host.Close();


        }

8  添加服务引用

9 实现 

 private void button1_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference2.WCFServiceClient client = new ServiceReference2.WCFServiceClient();
            client.DoDevideCompleted += new EventHandler<ServiceReference2.DoDevideCompletedEventArgs>(client_DoDevideCompleted);
            string errorMessage = String.Empty;
            client.DoDevideAsync(5, 0,errorMessage);
         
        }
      
        void client_DoDevideCompleted(object sender, ServiceReference2.DoDevideCompletedEventArgs e)
        {
            MessageBox.Show(e.errorMessage);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.WCFServiceClient client = new ServiceReference1.WCFServiceClient();
            client.DevideCompleted += new EventHandler<ServiceReference1.DevideCompletedEventArgs>(client_DevideCompleted);
            Exception ex = new Exception();
            client.DevideAsync(5, 0, ex);
        }

        void client_DevideCompleted(object sender, ServiceReference1.DevideCompletedEventArgs e)
        {
            MessageBox.Show(e.error.Message);
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值