【问题标题】:WCF Client in VS2005VS2005 中的 WCF 客户端
【发布时间】:2015-01-05 10:17:16
【问题描述】:

我正在尝试在 VS2005 项目中实现 WCF 客户端。在 VS2010 中一切正常,因为我可以添加服务引用。对于 VS2005,我使用 svcutil 构建了 .config 和 Service1.cs。这两个文件已添加到项目中,我还将服务引用添加为 Web 引用。 进一步你会发现生成的两个文件。

  <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="SDTRfServerClass.WCFService.WSHttpBinding_IService1" />
            </wsHttpBinding>
        </bindings>
        <client>
        <endpoint address="http://192.168.0.102:8732/Design_Time_Addresses/Calc/Service1/"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
          contract="SDTRfServerClass.WCFService.IService1" name="WSHttpBinding_IService1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>

这是我也尝试将其更改为 web.config 的 app.config,但这没有任何区别。

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName = "IService1")]
public interface IService1
{

    [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService1/Add", ReplyAction = "http://tempuri.org/IService1/AddResponse")]
    double Add(double n1, double n2);

    [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService1/Substract", ReplyAction = "http://tempuri.org/IService1/SubstractResponse")]
    double Substract(double n1, double n2);

    [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService1/Multiply", ReplyAction = "http://tempuri.org/IService1/MultiplyResponse")]
    double Multiply(double n1, double n2);

    [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService1/Divide", ReplyAction = "http://tempuri.org/IService1/DivideResponse")]
    double Divide(double n1, double n2);
}

这是 Service1.cs 的一部分。 当我尝试使用以下代码调用服务时,我收到错误“在 ServiceMode-client 的配置部分找不到名为 WSHttpBinding_IService1 和合同 IService1 的端点元素。如果应用程序的配置不是找到或发现了一个与名称相等的端点。 我试图在所有地方用完整的解决方案名称更改名称。 有没有人作为解决方案?

client = new Service1Client("WSHttpBinding_IService1");
double result = client.Add(Double.Parse("2"), Double.Parse("4"));

我也尝试使用 VS2005 的插件添加创建 .map 和 .cs 文件的服务引用,但我一直收到同样的错误。

【问题讨论】:

  • 您使用的是哪个版本的 .NET?

标签: wcf client visual-studio-2005 svcutil.exe


【解决方案1】:

我认为您有一些拼写错误或命名空间错误,因为错误意味着 .NET 无法找到有关您的端点的信息。

尝试发布您的完整 web.config,我已经查看了它

或者你可以像这样使用动态通道工厂,不用担心链接到你的 web.config

WSHttpBinding myBinding = new WSHttpBinding();

//define endpoint url (where service is deployed)
EndpointAddress myEndpoint = new EndpointAddress("http://192.168.0.102:8732/Design_Time_Addresses/Calc/Service1/"); //change to real endpoint 

//Use channel factory instead of generated one
ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(myBinding, myEndpoint); //Change to you WCF interface
IService1 myServiceClient = myChannelFactory.CreateChannel();

//and call it            
var result = myServiceClient.Add(1,1); //input to your method
//other methods

((IClientChannel)myServiceClient).Close();
myChannelFactory.Close();

【讨论】:

  • 谢谢!在VS2005的WCF客户端中使用WCF服务器功能的好方法!
猜你喜欢
  • 1970-01-01
  • 2012-01-05
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多