zoukankan      html  css  js  c++  java
  • WCF跨域 客户端无法访问

    尝试向 URI“http://localhost:8554/CustomerManage.svc”发出请求时出错。这可能是由于试图以跨域方式访问服务而又没有正确的跨域策略,或策略不适用于 SOAP 服务。您可能需要与该服务的所有者联系,以发布跨域策略文件并确保该文件允许发送 SOAP 相关的 HTTP 标头。出现此错误也可能是由于使用的是 Web 服务代理中的内部类型而没有使用 InternalsVisibleToAttribute 属性。有关详细信息,请参阅内部异常。

    刚开始我以为是把服务写错了。。但是服务在WPF程序中是可以调用的。。在Silverlight调用确发生错误。。

    在Silverlight错误的原因可能是:在默认情况下只允许源站点通信。

    在WCF服务那边放两个文件:

    只需要在你的*.Web服务端的根目录下放上两个文件clientaccesspolicy.xml 和crossdomain.xml

    XML文件:

    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>
    crissdomain.xml
    <?xml version="1.0"?> 
    <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> 
    <cross-domain-policy> 
    <allow-access-from domain="*" /> 
    <allow-http-request-headers-from domain="*" headers="*" /> 
    </cross-domain-policy> 

    我这边的实例:
    WCF服务:

    服务契约
    namespace WCFDemo
    {
        //服务契约
        [ServiceContract]
        public interface ICustomerManage
        {
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            List<CustomerInfo> GetAllCustomer();
    
        }
    
        //数据契约
        [DataContract]
        public class CustomerInfo
        {
            [DataMember(Order = 1)]
            public string CustomerID { get; set; }
            [DataMember(Order = 2)]
            public string CustomerName { get; set; }
            [DataMember(Order = 3)]
            public string CustomerPhone { get; set; }
            [DataMember(Order = 4)]
            public string CustomerAddress { get; set; }
        }
    }
    服务
    namespace WCFDemo
    {
        // 
        public class CustomerManage : ICustomerManage
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
    
            public List<CustomerInfo> GetAllCustomer()
            {
              return new List<CustomerInfo>()
                  {
                      new CustomerInfo(){CustomerAddress = "四川成都高新区",CustomerID = "00001",CustomerName = "唐纳德",CustomerPhone = "454d7782112"},
                      new CustomerInfo(){CustomerAddress = "四川成都高新区",CustomerID = "00002",CustomerName = "高峰期",CustomerPhone = "454d7782112"},
                      new CustomerInfo(){CustomerAddress = "四川成都高新区",CustomerID = "00003",CustomerName = "力保",CustomerPhone = "454d7782112"}
                  };
            }
        }
    }

    Silverlight客户端:

    XAML
    <controls:ChildWindow x:Class="SLMvvmDemo2.Views.ChildWindow1"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
               xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
               Width="622" Height="291"  Style="{StaticResource ChildWindowStyle}"
               Title="Silverlight 调用WCF " xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
        <Grid x:Name="LayoutRoot" Margin="2">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
    
            <Button x:Name="CancelButton" Content="取消" Click="CancelButton_Click" Width="75" Height="28" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
            <Button x:Name="OKButton" Content="确定" Click="OKButton_Click" Width="75" Height="28" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
            <sdk:DataGrid  Name="dataGrid1"  />
        </Grid>
    </controls:ChildWindow>
    程序代码
     public partial class ChildWindow1 : ChildWindow
        {
            public ChildWindow1()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(ChildWindow1_Loaded);
            }
    
            void ChildWindow1_Loaded(object sender, RoutedEventArgs e)
            {
                CustomerManageClient customer =new CustomerManageClient();
    
                customer.GetAllCustomerCompleted += customer_GetAllCustomerCompleted;
                customer.GetAllCustomerAsync();
    
            }
    
            void customer_GetAllCustomerCompleted(object sender, GetAllCustomerCompletedEventArgs e)
            {
                List<CustomerInfo> list = e.Result.ToList();
                dataGrid1.ItemsSource = list;
            }
    
            private void OKButton_Click(object sender, RoutedEventArgs e)
            {
                this.DialogResult = true;
            }
    
            private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
                this.DialogResult = false;
            }
        }

    WebConfig:

    WEBConfig
    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="BasicHttpBinding_ICustomerManage" maxBufferSize="2147483647"
                        maxReceivedMessageSize="2147483647">
                        <security mode="None" />
                    </binding>
                </basicHttpBinding>
            </bindings>
            <client>
                <endpoint address="http://localhost:8554/CustomerManage.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICustomerManage"
                    contract="CustomerService.ICustomerManage" name="BasicHttpBinding_ICustomerManage" />
            </client>
        </system.serviceModel>
    </configuration>

  • 相关阅读:
    spark 之 UDF的两种方式
    hive到hive数据迁移
    离线数据分析之 人物兴趣取向分析(2-3) 使用pyspark构建Kmeans/随机森林完成分类预测
    python 之sparkSQL连接hive
    hive面试题之 统计最近七天内连续登陆3天的用户数量
    离线数据分析之 人物兴趣取向分析(2-2)离线/实时项目架构|项目流程|数仓构建(进阶篇)
    离线数据分析之 人物兴趣取向分析(2-1)数据探索
    hive 和 hbase的联系
    linux常用命令
    centos8联网问题
  • 原文地址:https://www.cnblogs.com/w2011/p/2801529.html
Copyright © 2011-2022 走看看