[已解决问题] 在.NET 2008下创建WCF的程序是否会出错的?
提问时间: 2008-07-21 23:12
悬赏分: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();
            }
            

        }

    }

}

HelloClient.cs的代码如下:
 
/*
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
    }

}

 

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>

最奇怪的是我把这些拷贝到我以前用.NET 2005打了SP1的测试例子程序里面却能运行好好的。我以为是Frame版本不一样吧,但是又不能改成Frame 2.0的,改了就不能引用System.ServiceModel了。哎,不知道谁又遇到过这种情况的?我又不知如何上传附件,晕,只好把代码粘贴出来了。
所有回答(5)
没有问题,你搞一个干净的系统再装好2008,测试一下。
2个月前   回答者:datasky - 小虾三级
VS2008是没有问题,是不是你的系统有其他的东西没有卸载干净造成的呀?
2个月前   回答者:金鱼 - 小虾三级
出这个问题,通常是类的静态构造函数,或者静态字段赋值出错了。你可以反编译一下ClientBase看一下,哪儿可能出错。
2个月前   回答者:玉开 - 大侠五级
为什么不用vs2008自己生成的客户端代码?
2个月前   回答者:sumer - 初学一级
这是vs2008的Bug,当你更新引用时,他会在你的app.config里加入一条重复的endpoint,自已动手删一个吧
2个月前   回答者:绝世无才 - 菜鸟二级
评论
2个月前   很忙的闲人 :
我上网搜索过有些资料,也是说有关静态构造函数的问题,也确实是在HelloClient : ClientBase<IHello>, IHello
这个类的构造函数运行的时候出现这个错误的。我真不知道如何解决!
2个月前   很忙的闲人 :
我对比过了引用的文件,以及文件的版本号,我还试过把就客户端的项目引进到那个可以运行的工程里面,都还是出现这个错误。我把能正常运行的项目的引用都删掉,重新引用,也是能正常运行的。哎~~~~~见鬼了!
2个月前   很忙的闲人 :
vs2008自己生成的客户端代码?
我用那个工具,提示说不支持该中文语言!
   您需要登录以后才能回答!
 

我要提问

我的问题


快到期问题

> 问题排行榜

相关内容

相关链接