分享

MyService WCF服务应用程序

 牛人的尾巴 2016-02-22
出现了一个奇怪问题
我创建的全部都是新的
重创建一个空的.sln文件开始说
然后创建了一个WCF
最后创建了一个winform窗体
项目结构
WCFApplication.sln空解决方案
MyService WCF服务应用程序
WinForm项目

MyService里面文件
接口服务名

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace MyService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ITestService”。
    [ServiceContract]
    public interface ITestService
    {
        [System.ServiceModel.OperationContractAttribute(Action = "http:///ITestService/MyTest", ReplyAction = "http:///ITestService/MyTestResponse")]
        string MyTest(string _test);
    }
}
实现服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace MyService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“TestService”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 TestService.svc 或 TestService.svc.cs,然后开始调试。
    public class TestService : ITestService
    {
        public string MyTest(string _test)
        {
            return "Say WCF" + _test;
        }
    }
}

//在这里我只是声明一个MyTest方法,去实现了这个方法
Winform中代码
问题如下 我在Client(winform)调用时候出现了一个很奇怪的问题
winform

private void Form1_Load(object sender, EventArgs e)
        {
            ServiceHost serviceHost = new ServiceHost(typeof(Service.TestServiceClient));
            serviceHost.AddServiceEndpoint(typeof(Service.ITestService), new WSHttpBinding(), "http://localhost:16053/MyService/TestService");
            if (serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
            {
                ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
                serviceBehavior.HttpGetEnabled = true;
                serviceBehavior.HttpGetUrl = new Uri("http://localhost:16053/MyService/TestService/wsdl");
                serviceHost.Description.Behaviors.Add(serviceBehavior);
            }
            serviceHost.Open();
            Service.TestServiceClient client = new Service.TestServiceClient();

            client.MyTest("test");
            //client.MyTestAsync("test");//多出来这个方法。
        }
自动调用WCF后生成的App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ITestService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:16053/TestService.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_ITestService" contract="Service.ITestService"
                name="BasicHttpBinding_ITestService" />
        </client>
    </system.serviceModel>
</configuration>
serviceHost.Open()异常
“System.InvalidOperationException”类型的未经处理的异常在 System.ServiceModel.dll 中发生 

其他信息: 同一个协定中不能存在两个名称相同的操作,类型为 WindowsFormsApplication1.Service.ITestService 的方法 MyTestAsync 和 MyTest 违反了此规则。可以通过更改方法名称或使用 OperationContractAttribute 的 Name 属性更改其中一个操作的名称。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多