none
winform control嵌入ASPX页面后与WCF的通信问题 RRS feed

  • 问题

  • 情况是这样,我有一个WindowsFormsControlLibrary,还有一个WCFService,寄宿在IIS下,并且通过svcutil.exe生成了Service1.cs和config文件,然后将其添加至WindowsFormsControlLibrary的项目里,WindowsFormsControlLibrary的UserControl1.cs的代码如下

    Service1Client client = new Service1Client(new WSHttpBinding(), new EndpointAddress("http://sne-8080test/vmfile/Service1.svc"));
                MessageBox.Show(client.GetSKName(textBox1.Text));
                client.Close();
    

    这个时候,编译运行,是可以从服务端得到数据的。

    然后我将这个控件生成dll,嵌入到website的一个aspx页面中,

    <object id="object" classid="WindowsFormsControlLibrary1.dll#WindowsFormsControlLibrary1.UserControl1" width="150" height="150"></object> 
    


    然后将svcutil.exe生成的config文件的内容拷贝到website的webconfig文件中,

    <system.serviceModel>
            <bindings>
                <wsHttpBinding>
                    <binding name="WSHttpBinding_IService1" 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="65536"
                        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://sne-8080test/vmfile/Service1.svc" binding="wsHttpBinding"
                    bindingConfiguration="WSHttpBinding_IService1" contract="IService1"
                    name="WSHttpBinding_IService1">
                    <identity>
                        <servicePrincipalName value="host/sne-8080test" />
                    </identity>
                </endpoint>
            </client>
        </system.serviceModel>
    


    然后,编译运行,报错,

    The WSHttpBinding with name WSHttpBinding failed validation because it contains a BindingElement with type System.ServiceModel.Channels.SymmetricSecurityBindingElement which is not supported in partial trust. Consider disabling the message security and reliable session options, using BasicHttpBinding, or hosting your application in a full-trust environment.

     

    麻烦问一下各位高手,这是什么情况?该如何解决,谢谢。

    2011年9月14日 3:52

全部回复

  • 跟安全验证有关系。提示信息里页说对称加密不支持部分信任。

    你可以把 <security mode="Message">
    修为为 <security mode="None">


    Frank Xu Lei--谦卑若愚,好学若饥
    专注于.NET平台下分布式应用系统开发和企业应用系统集成
    Focus on Distributed Applications Development and EAI based on .NET
     

    老徐的网站】:http://www.frankxulei.com/

    微软WCF中文技术论坛
    微软WCF英文技术论坛

    Windows Azure中文技术论坛

    2011年9月15日 14:50
    版主
  • 用basicHttpBinding。
    2011年9月19日 3:14
  • 这几天单位的网络不好,总是上不来msdn,今天总算好了。

    我按照您说的将客户端和服务端的都改成了“None”,可是还是报一样的错误。不知道为什么。

    2011年9月21日 7:59
  • 你好,根据你的提示,我将WCF的config里的
    <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1">
    


    改成了basicHttpBinding,然后将控件里的代码改成了

    ServiceReference2.Service1Client sc = new WindowsFormsControlLibrary1.ServiceReference2.Service1Client(new BasicHttpBinding(), new EndpointAddress(<a href="http://localhost:2792/Service1.svc" rel="nofollow">http://localhost:2792/Service1.svc</a>));
    

    编译,然后将其dll拷贝至我的asp.net项目里,在页面上引用,结果又报出下面的错误:

    System.Security.SecurityException: 请求“System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”类型的权限已失败。

    哦,说明一下,在wcf里,我访问了数据库,是不是微软的机制不允许访问?

    2011年9月21日 8:23
  • 这里有个一样的错误

    http://social.msdn.microsoft.com/Forums/en/wcf/thread/22e53716-d4ff-4a1a-8801-6bfefd1f9551

    Solution:

    1. In the web.config for the WcfService1 project, change the endpoint binding form the default wsHttpBinding to basicHttpBinding.  Look under configuration/system.serviceModel/services/service and you will see the endpoint element
    2. Open the project properties page for the WcfService1 project.
    3. Set the "Secific port" property so an available port number.  This will prevent issues later on.
    4. Under the "Web" section (look to the immediate left), change the "Specific Page" value to ClientBin/WpfBrowserApplication1.xbap
    5. Open the WcfService1 project properties page
    6. You need to perform on of these steps so you can reference the XBAP via the http://localhost path.
      1. Under the Build Events section, set the Post-build event command line so it copies all of the output file to a folder within the WcfService1 project's root directory.  In my sample, I created a folder called ClientBin and used XCOPY to copy all of the output file to ClientBin.
      2. A simpler way to go is to change the Output path property under the Build section to a folder called ClientBin within the WcfService1 project's root folder.
    7. Consume the Service1.svc service in the WpfBrowserApplication1 like you normally would and make a call to the GetData method.  The somplest way to do this is to create a button in the Page1.xaml form and make the call in its Click event.  Make sure to output the return value of the service call to a MessageBox or something so you can know that the call succeeded. 
    8. Set the WcfService1 project as the StareUp project.
    9. Hit F5, click the button and you are done. 

    Hope this helps.

     

    参考其中的一些解决步骤,再试试


    Frank Xu Lei--谦卑若愚,好学若饥
    专注于.NET平台下分布式应用系统开发和企业应用系统集成
    Focus on Distributed Applications Development and EAI based on .NET
     

    老徐的网站】:http://www.frankxulei.com/

    微软WCF中文技术论坛
    微软WCF英文技术论坛

    Windows Azure中文技术论坛

    2011年9月22日 9:50
    版主
  • 在b/s应用程序中承载Winform控件,类似于OCX的机制。

    浏览器对页面上的这种控件,是部分信任的。

    你尝试改动一下浏览器中关于ActiveX的信任级别,

    比如说,全部设置成“启用”(我这个是针对IE说的),再运行看看。

     

    最好使用C++来开发这种ActiveX,然后进行正确的注册,

    不然老会面对这种部分信任的问题。

    2011年10月1日 7:06
  • 你好,根据你的提示,我将WCF的config里的
    <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1">
    


    改成了basicHttpBinding,然后将控件里的代码改成了

     

    ServiceReference2.Service1Client sc = new WindowsFormsControlLibrary1.ServiceReference2.Service1Client(new BasicHttpBinding(), new EndpointAddress(<a href="http://localhost:2792/Service1.svc" rel="nofollow">http://localhost:2792/Service1.svc</a>));
    

    编译,然后将其dll拷贝至我的asp.net项目里,在页面上引用,结果又报出下面的错误:

     

    System.Security.SecurityException: 请求“System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”类型的权限已失败。

    哦,说明一下,在wcf里,我访问了数据库,是不是微软的机制不允许访问?


    这个错误的含义好像是指不允许在asp.net中访问web服务,你可以使用webclient来调用web服务,尝试为wcf操作契约标记为webinvoke.
    2011年10月8日 3:31