悬赏分:50 浏览:239 次
我在.NET2008下创建了WCF的程序做测试,发现客户端会出现如下错误:
“System.ServiceModel.ClientBase`1”的类型初始值设定项引发异常。
我的代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Hello.WCFService.Contract;
namespace Hello.WCFService.Client

{
class Program
{
static void Main()
{
try
{
using (Hello.WCFService.Client.HelloClient MyHello = new Hello.WCFService.Client.HelloClient("Hello_http"))
{
Console.WriteLine("Begin to invocate the calculator Service");
Console.WriteLine("Wellcome to WCF World.{0}", MyHello.SayHello("Friend"));
Console.Read();
}
}
catch (Exception ex)
{
Console.WriteLine("StackTrace:{0}", ex.StackTrace);
Console.WriteLine("Message:{0}", ex.Message);
Console.Read();
} 
}
}
}

/**//*
using Hello.WCFService.Contract;
using System.ServiceModel;
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Hello.WCFService.Contract;

namespace Hello.WCFService.Client

{
class HelloClient : ClientBase<IHello>, IHello
{
internal HelloClient()
: base()
{ }
public HelloClient(string endpointConfigurationName)
: base(endpointConfigurationName)
{ }
public HelloClient(Binding binding, EndpointAddress address)
: base(binding, address)
{ }
IHello Members#region IHello Members
public string SayHello(string Name)
{
return this.Channel.SayHello(Name);
}
#endregion
}
}
App.config配置文件如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.ServiceModel>
<Client>
<endpoint address="http://localhost:8080/HelloService" binding="basicHttpBinding" contract="Hello.WCFService.Contract.IHello" name="Hello_http"/>
<Endpoint Address="http://localhost:8080/HelloService" Binding="basicHttpBinding" contract="Hello.WCFService.Contract.IHello" name="selfHostEndpoint_http"/>
</Client>
</system.ServiceModel>
</configuration>
|
没有问题,你搞一个干净的系统再装好2008,测试一下。 VS2008是没有问题,是不是你的系统有其他的东西没有卸载干净造成的呀? 出这个问题,通常是类的静态构造函数,或者静态字段赋值出错了。你可以反编译一下ClientBase看一下,哪儿可能出错。 为什么不用vs2008自己生成的客户端代码? 这是vs2008的Bug,当你更新引用时,他会在你的app.config里加入一条重复的endpoint,自已动手删一个吧 |
|
2个月前 很忙的闲人 : 我上网搜索过有些资料,也是说有关静态构造函数的问题,也确实是在HelloClient : ClientBase<IHello>, IHello 这个类的构造函数运行的时候出现这个错误的。我真不知道如何解决! |
|
2个月前 很忙的闲人 : 我对比过了引用的文件,以及文件的版本号,我还试过把就客户端的项目引进到那个可以运行的工程里面,都还是出现这个错误。我把能正常运行的项目的引用都删掉,重新引用,也是能正常运行的。哎~~~~~见鬼了! |
|
2个月前 很忙的闲人 : vs2008自己生成的客户端代码? 我用那个工具,提示说不支持该中文语言! |