none
请问在流模式的wcf程序客户端,为何要创建通道,才能调用服务端的函数? RRS feed

  • 问题

  •    ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client();
                 ServiceReference1.IService1 channel = sc.ChannelFactory.CreateChannel();//创建通道。 
                 channel.UploadStream(request1);

    而不能如下直接调用函数

    sc.UploadStream(request1);


    • 已编辑 尺蠖 2013年11月18日 6:51
    2013年11月18日 6:50

答案

  • 如果你要直接使用代理类,用:

    System.IO.Stream stream = FileUpload1.PostedFile.InputStream;
                ser.UploadFile(FileUpload1.PostedFile.FileName, "pppp", stream);

    因为代理类中没有只有一个参数的UploadFile方法。里面的方法是:

      public void UploadFile(string FileName, string SavePath, System.IO.Stream FileData) {
                UpLoadWebApplication.UpLoadService.FileUploadMessage inValue = new UpLoadWebApplication.UpLoadService.FileUploadMessage();
                inValue.FileName = FileName;
                inValue.SavePath = SavePath;
                inValue.FileData = FileData;
                ((UpLoadWebApplication.UpLoadService.IUpLoadService)(this)).UploadFile(inValue);
            }
    当你创建通道,用的是IUpLoadService, 该方法的定义是:
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
        [System.ServiceModel.ServiceContractAttribute(ConfigurationName="UpLoadService.IUpLoadService")]
        public interface IUpLoadService {
            
            // CODEGEN: Generating message contract since the wrapper name (FileUploadMessage) of message FileUploadMessage does not match the default value (UploadFile)
            [System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="UploadFile")]
            void UploadFile(UpLoadWebApplication.UpLoadService.FileUploadMessage request);
        }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 尺蠖 2013年11月26日 9:30
    2013年11月25日 7:03
    版主

全部回复

  • 你好,

    我这边做了一个简单的测试,使用流模式似乎不需要创建通道就可以调用函数。

    当你不创建通道调用函数,报错了吗?什么错?


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年11月21日 12:44
    版主

  • 是啊,直接写是报错的

    2013年11月22日 9:58
  • 你好,

    出现这个错误,有可能是没有引用定义了服务和契约的项目。如图所示


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年11月25日 1:40
    版主
  • 2013年11月25日 2:15
  • 如果你要直接使用代理类,用:

    System.IO.Stream stream = FileUpload1.PostedFile.InputStream;
                ser.UploadFile(FileUpload1.PostedFile.FileName, "pppp", stream);

    因为代理类中没有只有一个参数的UploadFile方法。里面的方法是:

      public void UploadFile(string FileName, string SavePath, System.IO.Stream FileData) {
                UpLoadWebApplication.UpLoadService.FileUploadMessage inValue = new UpLoadWebApplication.UpLoadService.FileUploadMessage();
                inValue.FileName = FileName;
                inValue.SavePath = SavePath;
                inValue.FileData = FileData;
                ((UpLoadWebApplication.UpLoadService.IUpLoadService)(this)).UploadFile(inValue);
            }
    当你创建通道,用的是IUpLoadService, 该方法的定义是:
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
        [System.ServiceModel.ServiceContractAttribute(ConfigurationName="UpLoadService.IUpLoadService")]
        public interface IUpLoadService {
            
            // CODEGEN: Generating message contract since the wrapper name (FileUploadMessage) of message FileUploadMessage does not match the default value (UploadFile)
            [System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="UploadFile")]
            void UploadFile(UpLoadWebApplication.UpLoadService.FileUploadMessage request);
        }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 尺蠖 2013年11月26日 9:30
    2013年11月25日 7:03
    版主
  • 试了一下确实成功,这种MessageContract定义的类也真是奇怪了,在代理类是拆成几个参数了。

    真是非常感谢  Haixia Xie - MSFT 的耐心回复


    • 已编辑 尺蠖 2013年11月26日 8:26
    2013年11月26日 8:24
  • 另外我还有个问题,我上传一个56K的文件,就报错,38K没问题,这还是服务端或者客户端配置不对吗?

    报错是 :远程服务器返回了意外响应: (400) Bad Request。

    服务端配置

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
     
        <system.web>
        <httpRuntime maxRequestLength="12097151"/>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <services>
          <!--<service  behaviorConfiguration="StreamingServiceBehavior" name="Microsoft.ServiceModel.Samples.StreamingService"  >-->
          <service  name="Microsoft.ServiceModel.Samples.StreamingService"  >
           
            
          <endpoint  address="" binding="basicHttpBinding" bindingConfiguration="HttpStreaming" contract="IService1"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
    
        <bindings>
           <!--an example basicHttpBinding using streaming-->
          <basicHttpBinding>
            <binding name="HttpStreaming"   messageEncoding="Mtom"    maxBufferPoolSize="2147483647"  maxReceivedMessageSize="9223372036854775807"  transferMode="Streamed"
                     />
            
          </basicHttpBinding>
          
           
        </bindings>
    
        <behaviors>
          <serviceBehaviors>
            <behavior>
               <!--为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点--> 
              <serviceMetadata httpGetEnabled="true"/>
               <!--要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息--> 
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
     
          <serviceBehaviors>
            <behavior name="StreamingServiceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>-->
         
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
     <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>
      
    </configuration>

    客户端配置

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
                        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                        maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                        useDefaultWebProxy="true">
                        <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                            maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                        <security mode="None">
                            <transport clientCredentialType="None" proxyCredentialType="None"
                                realm="" />
                            <message clientCredentialType="UserName" algorithmSuite="Default" />
                        </security>
                    </binding>
                </basicHttpBinding>
            </bindings>
            <client>
                <endpoint address="http://localhost:14098/Service1.svc" binding="basicHttpBinding"
                    bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
                    name="BasicHttpBinding_IService1" />
            </client>
        </system.serviceModel>
    </configuration>



    • 已编辑 尺蠖 2013年11月26日 8:37
    2013年11月26日 8:35
  • 你好,

    maxReceivedMessageSize可设置的最大值为2147483647,你设置为"9223372036854775807"是不起作用的。传输大文件时WCF配置过程中要注意:

    1. 在使用WCF服务的Client端的配置文件中,记得设置maxBufferSize的值和maxReceivedMessageSize的值,不然文件一大就报错了,这里的单位是字节。

    2. 在编写WCF服务的Server端添加bindings节点,给相应的Service指定bindingConfiguration属性。

    详情可参考:
    http://www.cnblogs.com/cuihongyu3503319/archive/2010/09/01/1814394.html

    鉴于你的原问题解决了,如果有新问题的话,建议你重新开一个帖子。 :)

    谢谢理解~


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年11月26日 8:52
    版主
  • 设置为2147483647还是不行啊,行,我来开个新帖吧
    2013年11月26日 9:30