我只试验成功了用TCP在本机的访问,但是不能远程连TCP,用Http本地连也不行,Error404找不到资源。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ServiceModel; using System.Runtime.Serialization; namespace Cdm.Security.Service { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); // TODO: Add your service operations here } // Use a data contract as illustrated in the sample below to add composite types to service operations [DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } } public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } } }
配置:
<system.serviceModel> <services> <service name="Cdm.Security.Service.Service1"> <endpoint address="" binding="wsHttpBinding" contract="Cdm.Security.Service.IService1"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080/service1/" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8080/service1/mex" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
不能远程TCP的话,估计是防火墙没打开。比如:tcp://192.168.6.1:8000
你得保证外部能使用 192.168.6.1 这个地址访问服务器,且 8000 端口允许入栈、出栈。
HTTP也一样,需要注意的是,如果同IIS上的站点的地址重复的话,需要将WCF中的设置端口共享打开.
<system.serviceModel> <services> <service name="Cdm.Security.Service.Service1"> <endpoint address="" binding="wsHttpBinding" contract="Cdm.Security.Service.IService1"> <identity> <dns value="scx-tomy" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://scx-tomy/service1/" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://scx-tomy/service1/mex" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
以上是服务端配置,以下是客户端配置,我觉得可能是配置上出了问题
<system.serviceModel> <client> <endpoint address="http://scx-tomy/service1/" binding="wsHttpBinding" contract="IService1" name="DefaultBinding_IService1_IService1"> <identity> <dns value="scx-tomy" /> </identity> </endpoint> </client> </system.serviceModel>
@灰灰狼: 部署到IIS中测试下。
@程序猿.码农: 放IIS里了,还是404
@灰灰狼: 补充了完整代码
@灰灰狼: 如果你换成 basicHttpBinding,能运行吗?