WCF碰到的一些问题

static ServiceHost myService = null; 

myService = new ServiceHost(typeof(Service1));

 myService.Open();

问题1

Service 'WcfServiceLibrary1.Service1' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

 

出现这个问题的原因是因为没有给服务器(host)端配置config,在服务器端添加个config文件,从WCF那里把system.serviceModel节点复制到你添加的config文件里就可以解决了。

 

问题2

HTTP could not register URL http://+:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/. Another application has already registered this URL with HTTP.SYS.

 

出现这个问题的原因是从WCF服务里直接复制过来的,在服务那里已经被注册过了,而新写的服务器端又重新注册一遍,导致这样的问题,只需要把8732改为任何一个没有用的端口都可以。

至于如何WCF服务那里注册过,这里再打开就不行,不知道原因。如果有知道的高手,希望指教。

 

问题3

HTTP could not register   http://+:8581/Design_Time_Addresses/WcfServiceLibrary1/Service1/. Your process does 
not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

 

出现这个问题的原因是权限不够。

如果只是单机的系统管理员登陆是不会出现这个问题,当用域用户登录的时候会发生,

打开 程序-〉附件-〉命令提示符(Command Prompt Command Prompt )(注意需要右键命令提示符,悬在Run as administrator)

输入如下语句注册。

netsh http add urlacl url=http://+:8581/Design_Time_Addresses/WcfServiceLibrary1/Service1/ user=域名/用户名

 

可以用各种方式来创建服务器端,

假设用winform

 

 StartService();

label1.Text = "this service is runing";
 //StopService();

注意这个winform程序部署的时候config需要配置一个,

假设一个项目里有个3个工程,Service项目,主机项目,客户端项目

在调试状态的时候,客户端项目引用的是Service项目,

地址如:http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/mex

但是主机项目里config配置的端口肯定不能是8732,所以导致出现这样问题,

调试状态没有问题,可以在部署环境,打开主机程序和客户端程序,但是在客户端操作的时候报错,无法找到主机,

这个是因为主机的config不是 8732,主要把主机的config配置里端口号改为8732就行了 。 

问题3:

TCP error code 10061: 由于目标机器积极拒绝,无法连接http://localhost:8732/Design_Time_Addresses/TeacherHelperServic

当我们在客户端添加WCF服务引用的时候出错,信息如下:

TCP error code 10061: 由于目标机器积极拒绝,无法连接

http://localhost:8732/Design_Time_Addresses/TeacherHelperServic

       无法连接到远程服务器,由于目标机器积极拒绝,无法连接。 127.0.0.1:8732/

 

可能原因:

1.相应服务端口未开启

2.某些TCP 监听服务没有开启

3. Client与Serivce配置不匹配

解决方法参考:

 1.查看防火墙设置。有没有打开服务端口,比如8732,没有的话添加服务端口为安全端口;

 2.检查服务托管进程是否启动,这个情况一般是针对自定义宿主来托管服务的情况,运行服务托管程序。

 3.也许Net.Tcp Listener Adapter等服务没有启动。Net.Tcp Listener Adapter是设置为自动启动的。

问题4:

这可能是由于服务终结点绑定未使用 HTTP 协议造成的 原因数据量过大

今天又看到"这可能是由于服务终结点绑定未使用 HTTP 协议造成的"  这个蛋疼的错误  

这个错误之前也遇到过多次 归纳原因有

1、没有重新生成代理文件

2、这是因为WCF返回值无法序列化造成的

但我今天遇到时 我想这次肯定不是这两个原因之一  我想应该是数据量过大 于是把数据减少一部分后 一切正常 那就找解决方案 最后看到 这个帖子http://topic.csdn.net/u/20080708/08/8158eac6-08fd-4e04-b37d-7dfc8137a6d9.html?seed=937996111&r=60490402#r_60490402  按文中的配置方法配置后 问题解决。

既然是好东西 那我就“偷”来  呵呵

 

解决方法:
用SvcConfigEditor为客户端和服务端都加上一个behavior (服务端添加servicehehavior, 客户端添加endpointbehavior),然后为这两个服务各添加一个dataContractSerializer,把他的maxItemsInObjectGraph字段变成更大的数。

 

服务端配置:

<system.serviceModel>
     <diagnostics>
       <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
         logMessagesAtTransportLevel="true" />
     </diagnostics>
     <behaviors>
       <serviceBehaviors>
         <behavior name="NewBehavior">
           <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8000/ContactManager/md" />
           <serviceDebug includeExceptionDetailInFaults="true" />
           <dataContractSerializer maxItemsInObjectGraph="65536000" />        </behavior>
       </serviceBehaviors>
     </behaviors>
     <services>
       <service behaviorConfiguration="NewBehavior" name="WcfServiceLibrary1.ContactManager">
         <endpoint address="http://localhost:8000/ContactManager" binding="wsHttpBinding"
           bindingConfiguration="NewBindingStationCnfg" contract="WcfServiceLibrary1.IContactManager" />
         <endpoint address="http://localhost:8000/ContactManager/mex" binding="mexHttpBinding" contract="IMetadataExchange" />
       </service>
     </services>
     <bindings>
       <wsHttpBinding>
         <binding name="NewBindingStationCnfg" maxReceivedMessageSize="483647" />
       </wsHttpBinding>

     </bindings>
   </system.serviceModel>



客户端的config:

     
<system.serviceModel>
         <behaviors>
             <endpointBehaviors>
                <behavior name="NewBehavior">
                    <dataContractSerializer maxItemsInObjectGraph="6553600" />
                </behavior>
            </endpointBehaviors>        </behaviors>
         <diagnostics>
             <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
                 logMessagesAtTransportLevel="true" />
         </diagnostics>
         <bindings>
             <wsHttpBinding>
                 <binding name="WSHttpBinding_IContactManager1" closeTimeout="00:01:00"
                     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                     bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                     maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
                     messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                     allowCookies="false">
                     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                         maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                     <reliableSession ordered="true" inactivityTimeout="00:10:00"
                         enabled="false" />
                     <security mode="Message">
                         <transport clientCredentialType="Windows" proxyCredentialType="None"
                             realm="" />
                         <message clientCredentialType="Windows" negotiateServiceCredential="true"
                             algorithmSuite="Default" establishSecurityContext="true" />
                     </security>
                 </binding>
             </wsHttpBinding>
         </bindings>
         <client>
             <endpoint address="http://localhost:8000/ContactManager" behaviorConfiguration="NewBehavior"
                 binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IContactManager1"
                 contract="ServiceReference2.IContactManager" name="WSHttpBinding_IContactManager1">
                 <identity>
                     <userPrincipalName value="vblab1@redmond.corp.microsoft.com" />
                 </identity>
             </endpoint>
         </client>
     </system.serviceModel>

问题5:在服务 ObtainData 实现的协定列表中找不到协定名称 "IMetadataExchange"。将 ServiceMetadataBehavior 添加到配置文件或直接添加到 ServiceHost,以启用对该协定的支持。

第一种解决方法:最暴力的

配置去掉<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

第二种解决方法:

其实已经有了serviceBehavior,但是忘记绑定到Service上了

<div style="text-align: justify;"><span style="font-family: 'Microsoft YaHei';"><span style="font-size:10px;"><service behaviorConfiguration="serviceBehavior" name="ZBMService.ObtainData"></span></span></div><div style="text-align: justify;"><span style="font-family: 'Microsoft YaHei';"><span style="font-size:10px;"><endpoint address="ObtainData" binding="wsHttpBinding" bindingConfiguration="Binding1"</span></span></div><div style="text-align: justify;"><span style="font-size:10px;">contract="ZBMServiceContract.IObtainData" /></span></div><div style="text-align: justify;"><span style="font-family: 'Microsoft YaHei';"><span style="font-size:10px;">contract="IMetadataExchange" /></span></span></div><div style="text-align: justify;"><span style="font-size:10px;"><endpoint address="mex" binding="wsHttpBinding" bindingConfiguration=""</span></div><div style="text-align: justify;"><span style="font-family: 'Microsoft YaHei';"><span style="font-size:10px;"></service></span></span></div>

问题6:在客户端调用 服务, DataBaseServiceClient client = new DataBaseServiceClient() ? client=null  类型初始值设定项引发异常

原因:没有吧<system.serviceModel>配置 写入到客户端

问题7:无法打开安全通道,因为与远程终结点的安全协商已失败。这可能是由于用于创建通道的 EndpointAddress 中不存在 EndpointIdentity 或错误指定了 EndpointIdentity。请确认由 EndpointAddress 指定或暗示的 EndpointIdentity 正确标识了远程终结点。

遇到这个问题我的第一反应就是客户端和服务端的配置不同造成的。

如果是通过VS引用服务地址的方式添加的服务只要更新服务引用后这个问题就解决了。

但是如果是通过自己手写代码调用就要注意

检查一下EndpointAddress 中EndpointIdentity客户端服务器设置是否统一,另外确认两端的安全级别一致。

例如 :我在服务端配置中设置了

 <wsHttpBinding>
        <binding name="NoneSecurity"
          <security mode="None"/>
        </binding>
      </wsHttpBinding>

客户端代码中就要相同的定义安全级别,而不单单是binding和Address 还有Security  

  WSHttpBinding WShb = new WSHttpBinding();//使用协议与服务端相同
        WShb.Security.Mode = SecurityMode.None; //安全级别
        EndpointAddress epo = new EndpointAddress("http://192.168.1.159:8080/Service.svc");//指定WCF服务地址

WCF常见问题

一、创建时,WCF Service中HttpContext.Current为null的解决办法

1. 在hosting WCF的web.config中加入:

<system.serviceModel>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

</system.serviceModel>

2. 在Service的类定义上加上下面Attribute:

[AspNetCompatibilityrequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

 

二、错误提示:调用方未由服务进行身份验证。

修改WCF服务的web.config文件system.serviceModel下添加配置:

<bindings>

<wsHttpBinding>

    <binding name="bindingConfiguration1">

      <security mode="None">

        <transport clientCredentialType="None"/>

        <message clientCredentialType="None"/>

      </security>

    </binding>

</wsHttpBinding>

</bindings>

    </system.serviceModel>

</configuration>

并修改behaviors下的endpoint,添加bindingConfiguration="bindingConfiguration1"

 

三、已超过传入消息(65536)的最大消息大小配额

解决方案1:调用方的代码里加入:

(client.Endpoint.Binding as WSHttpBinding).MaxReceivedMessageSize = int.MaxValue;

解决方案2:修改客户端调用方的Config配置文件:

<system.serviceModel>

    <bindings>

        <wsHttpBinding>

            <binding maxReceivedMessageSize="65536000"

 

四、调用时服务端返回400 Bad Request错误

解决方案:修改服务端的Config配置文件,增加maxReceivedMessageSize设置:

<system.serviceModel>

    <bindings>

        <wsHttpBinding>

            <binding name="bindingConfiguration1" maxReceivedMessageSize="2147483647"

 

五、 无法打开安全通道,因为与远程终结点的安全协商已失败。

这可能是由于用于创建通道的 EndpointAddress 中不存在 EndpointIdentity 或错误指定了 EndpointIdentity。

请确认由 EndpointAddress 指定或暗示的 EndpointIdentity 正确标识了远程终结点。

这个错误通常是服务端相关配置修改了,删除引用,重新添加引用即可

 

六、接收对 http://xxx.svc 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。有关详细信息,请参阅服务器日志。

1、没有重新生成代理文件

2、这是因为WCF返回值无法序列化造成的

WCF的方法,不能返回Object、ICollection、IList之类的不明确的数据类型,但是IList<string>这样的类型可以返回,如果返回IList<SimpleSoft>这样的自定义类型,需要在接口上增加KnownType,如:

[ServiceContract]

[ServiceKnownType(typeof(SimpleSoft))]

public interface ISearchService

 

七、格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: 方法名。InnerException 消息是“在行 1、位置 1485 出现错误。 元素“http://schemas.datacontract.org /2004/07/父类”含有“http://schemas.datacontract.org/2004/07/子类”数据协定的数据。反序列化程序 不知道映射到此协定的类型。请将与“子类”对应的类型添加到已知类型的列表中,例如,通过使用 KnownTypeAttribute 属性或通过将其添加到传递给 DataContractSerializer 的已知类型的列表等方法。”。有关详细信息,请参阅 InnerException。

接口返回父类,但是实际返回的是子类,就会出现这个错误,解决方法,在父类定义上添加属性,如:

[KnownType(typeof(FullSoft))]

public class SimpleSoft

 

八、读取 XML 数据时,超出最大字符串内容长度配额 (8192)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额。

解决方案:修改客户端的Config配置文件,增加maxStringContentLength设置:

<system.serviceModel>

    <bindings><wsHttpBinding><binding name=""....>

<readerQuotas maxStringContentLength="2147483647"

 

九、无 法激活服务,因为它不支持 ASP.NET 兼容性。已为此应用程序启用了 ASP.NET 兼容性。请在 web.config 中关闭 ASP.NET 兼容性模式或将 AspNetCompatibilityRequirements 属性添加到服务类型且同时将 RequirementsMode 设置为“Allowed”或“Required”。

解决办法:

修改相应   服务.svc.cs

using System.ServiceModel.Activation ;

[AspNetCompatibilityRequirements (RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]

 

十、WCF接口的参数,如果是枚举值,则必须是已经定义的枚举

比如枚举定义: enum aaa{aa=1, bb=2} 如果参数有aaa类型,传递3就会出错,因为枚举定义里没有3

如果枚举定义加上Flags属性,就可以传递3了(等于是aaa.aa | aaa.bb)

[Flags]

enum aaa{aa=1, bb=2}

 

十一、作为WCF接口的参数,其成员必须有public的set属性,否则不会传递,比如下面的a参数可以在wcf中使用,下面的b参数无法使用:

public aaa{

    public int a{get;set;}

    public int b{get;private set;}

}

 

十二、System.ArgumentException: 此集合已经包含方案 http 的地址。此集合中每个方案中最多只能包含一个地址。 

WCF 针对每个schema只支持一个绑定,所以站点不能绑定多个主机头来使用WCF,如果绑定多个主机头,可以在Web.config里配置,但是如下配置后,其它主机头无法使用这个wcf:

<serviceHostingEnvironment>

    <baseAddressPrefixFilters>

        <add prefix="http://www.cnblogs.com/"/>

    </baseAddressPrefixFilters>

</serviceHostingEnvironment>

十三、在wcf项目下,找到web.config 文件修改如下(蓝色部分)

  <behaviors>
     <serviceBehaviors>
        <behavior>
          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
  </behaviors>







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值