WCF服务消耗问题

时间:2013-09-08 20:54:13

标签: c# wcf wcf-binding wcf-security

我有一个承载WCF服务的Windows服务。我有一个使用该服务的客户端。即使我将其添加为服务引用,客户端也无法识别ServiceReference1。

我一直在努力解决这个问题 - 我已经多次阅读过这段代码 - 之前我曾使用过WCF而没有任何问题(以与此相同的方式)。

接口

namespace ValidStateService
{
    [ServiceContract]
    public interface IValidStateWCFService
    {
        // Add two numbers
        [OperationContract(IsOneWay = false)]
        int AddNumbers(int numA, int numB);  

    }
}

接口的实施

namespace ValidStateService
{
    public class ValidStateWCFService : IValidStateWCFService
    {

        // Call the agent to add two numbers and return the result
        public int AddNumbers(int numA, int numB)
        {

            try
            {
                    return 20;  // Always return 20 for testing
            }
            catch (Exception ex)
            {
                return -1;
            }
        }    
    }
}

Windows服务App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="ValidStateService.ValidStateWCFService">
                <endpoint address="" binding="basicHttpBinding" contract="ValidStateService.IValidStateWCFService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8732/ValidStateService/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

现在在Winform上我添加了对服务的引用,但无法识别servicereference1。 代码添加两个数字 * Winform App.config *

private void buttonGetDataFromAgent_Click(object sender, EventArgs e)
{

    try
    {
        InstanceContext context = new InstanceContext(this);
        ServiceReference1.   // Winform App does not recognize this i.e no intellisense 

??????

客户端App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

  <system.serviceModel>
    <client>
      <endpoint address="http://localhost:8732/ValidStateService/"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IValidStateWCFService"
        contract="ServiceReference1.IValidStateWCFService" name="BasicHttpBinding_IValidStateWCFService" />
    </client>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IValidStateWCFService" />
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

0 个答案:

没有答案