0

我有一个托管 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 服务应用程序.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>
4

0 回答 0