首先发布Wcf到IIS上
在发布的时候 有时候IIS无法识别svc文件 就需要添加
然后就是添加跨域文件
1.clientaccesspolicy.xml
<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>
<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>
当发生跨域访问时 IIS会自动在根目录下寻找这两个文件:IIS默认跟目录C:\Inetpub\wwwroot直接添加上述两个文件
需要注意是:不是站点根目录 是IIS根目录
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
// TODO: Add your service operations here
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
ServiceReferences.ClientConfig
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://pc-200911231149/slwcf/slwcf.Service1.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="slwcf.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/Design_Time_Addresses/slwcf/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="basicHttpBinding" contract="slwcf.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
sl:
public MainPage()
{
InitializeComponent();
ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client();
sc.GetDataCompleted += new EventHandler<ServiceReference1.GetDataCompletedEventArgs>(sc_GetDataCompleted);
sc.GetDataAsync(123);
}
void sc_GetDataCompleted(object sender, ServiceReference1.GetDataCompletedEventArgs e)
{
MessageBox.Show(e.Result);
}
本文介绍如何将WCF服务部署到IIS,并解决跨域访问问题。通过添加必要的clientaccesspolicy.xml和crossdomain.xml文件到IIS根目录,确保服务能够正确响应跨域请求。此外还提供了服务接口定义和服务引用配置示例。


156

被折叠的 条评论
为什么被折叠?



