无法从命名空间“http://tempuri.org/”导入绑定“BasicHttpBinding_PROPrintBarCodeService”。

qq598235031 2013-06-18 06:24:46
客户端引用WCF 报错无法从命名空间“http://tempuri.org/”导入绑定“BasicHttpBinding_PROPrintBarCodeService”。
这是为啥?
情景:服务端原来是JAVA发布的服务,现在修改为.NET,我使用IIS 发布webservice是可以用的,但是使用winform 中嵌入WCF服务就不能使用,协议修改basicHttpBinding 。

服务端WCF配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="PROPrintBarCodeService_ZipBase64Service.BMServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="PROPrintBarCodeService_ZipBase64Service.BMServiceBehavior"
name="PROPrintBarCodeService_ZipBase64Service.PROPrintBarCodeService_ZipBase64Service" >
<endpoint address="http://localhost:10001/PROPrintBarCodeService_ZipBase64Service/"
binding="basicHttpBinding" contract="PROPrintBarCodeService_ZipBase64Service.PROPrintBarCodeService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:10001/PROPrintBarCodeService_ZipBase64Service/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>


客户端引用,大致如下
public static object InvokeWebService1(string url, string methodname, object[] args,string strUserName,string strUserPwd)
{
try
{
// 1. 使用 WebClient 下载 WSDL 信息。
//WebClient web = new WebClient();
////2012.8.20 yanglx add 加上网络凭证
//web.Credentials = new NetworkCredential(strUserName, strUserPwd);
//Stream stream = web.OpenRead(url);
// 2. 创建和格式化 WSDL 文档。
Stream stream = (new StreamReader(url)).BaseStream;
ServiceDescription description = ServiceDescription.Read(stream);
string strName = "";
if (description.Services.Count > 0)
{
//读取服务名
strName = description.Services[0].Name;
}
// 3. 创建客户端代理代理类。
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();

importer.ProtocolName = "Soap"; // 指定访问协议。
importer.Style = ServiceDescriptionImportStyle.Client; // 生成客户端代理。
importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;

importer.AddServiceDescription(description, null, null); // 添加 WSDL 文档。

// 4. 使用 CodeDom 编译客户端代理类。
CodeNamespace nmspace = new CodeNamespace(); // 为代理类添加命名空间,缺省为全局空间。
CodeCompileUnit unit = new CodeCompileUnit();
unit.Namespaces.Add(nmspace);
///////这个步骤抛出异常,无法从命名空间“http://tempuri.org/”导入绑定/// “BasicHttpBinding_PROPrintBarCodeService”。
ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

CompilerParameters parameter = new CompilerParameters();
parameter.GenerateExecutable = false;
parameter.GenerateInMemory = true;
parameter.ReferencedAssemblies.Add("System.dll");
parameter.ReferencedAssemblies.Add("System.XML.dll");
parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
parameter.ReferencedAssemblies.Add("System.Data.dll");
CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);

string ss = "";
// 5. 使用 Reflection 调用 WebService。
if (!result.Errors.HasErrors)
{
Assembly asm = result.CompiledAssembly;
Type t = null;
if (strName != "")
{
t = asm.GetType(strName, true, true);
}
else
{
string className = GetWsClassName(url);

t = asm.GetType(className, true, true);
} // 如果在前面为代理类添加了命名空间,此处需要将命名空间添加到类型前面。
object o = Activator.CreateInstance(t);
//设置访问的网络凭证
((System.Web.Services.Protocols.WebClientProtocol)(o)).Credentials = new NetworkCredential(strUserName, strUserPwd);

MethodInfo method = t.GetMethod(methodname);
ss = (string)method.Invoke(o, args);
}
return ss;
}
catch (Exception ex)
{
throw ex;
}
}
...全文
828 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧