首页 新闻 赞助 找找看

用Windows Service来承载WCF,协议用Http,有谁做过?

0
悬赏园豆:10 [已解决问题] 解决于 2012-08-11 14:08

我只试验成功了用TCP在本机的访问,但是不能远程连TCP,用Http本地连也不行,Error404找不到资源。

WCF
问题补充:
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>
灰灰狼的主页 灰灰狼 | 初学一级 | 园豆:5
提问于:2012-07-19 17:37
< >
分享
最佳答案
0

不能远程TCP的话,估计是防火墙没打开。比如:tcp://192.168.6.1:8000

你得保证外部能使用 192.168.6.1 这个地址访问服务器,且 8000 端口允许入栈、出栈。

HTTP也一样,需要注意的是,如果同IIS上的站点的地址重复的话,需要将WCF中的设置端口共享打开.

收获园豆:10
Launcher | 高人七级 |园豆:45045 | 2012-07-19 18:11
  <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>
灰灰狼 | 园豆:5 (初学一级) | 2012-07-20 14:59

@灰灰狼: 部署到IIS中测试下。

Launcher | 园豆:45045 (高人七级) | 2012-07-20 15:53

@程序猿.码农: 放IIS里了,还是404

灰灰狼 | 园豆:5 (初学一级) | 2012-07-20 17:26

@灰灰狼: 补充了完整代码

灰灰狼 | 园豆:5 (初学一级) | 2012-07-20 17:29

@灰灰狼: 如果你换成 basicHttpBinding,能运行吗?

Launcher | 园豆:45045 (高人七级) | 2012-07-20 17:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册