如何从Silverlight调用WCF Web服务?

时间:2022-10-30 23:19:01

I am trying to call a WCF webservice (which I developed) from a Silverlight application. For some reason the Silverlight app does not make the http soap call to the service. I know this because I am sniffing all http traffic with Fiddler (and it is not a localhost call).

我试图从Silverlight应用程序调用WCF Web服务(我开发)。由于某种原因,Silverlight应用程序不会对服务进行http soap调用。我知道这是因为我用Fiddler嗅探所有的http流量(并且它不是本地主机调用)。

This my configuration in the server relevant to WCF:

这是我在与WCF相关的服务器中的配置:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
        <service behaviorConfiguration="ServiceBehavior" name="Service">
            <endpoint address="" binding="basicHttpBinding" contract="Service"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
</system.serviceModel>

And the ServiceReferences.ClientConfig file in the silverlight app (i am using the beta 2):

Silverlight应用程序中的ServiceReferences.ClientConfig文件(我正在使用beta 2):

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_Service" maxBufferSize="65536"
                maxReceivedMessageSize="65536">
                <security mode="None" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://itlabws2003/Service.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_Service" contract="Silverlight_organigram.DataService.Service"
            name="BasicHttpBinding_Service" />
    </client>
</system.serviceModel>

This is the silverlight method that calls the service, I paste the whole method for copleteness, the lambda is to make the call synchronous, I have debugged it and after the line client.GetPersonsAsync(), Fiddler does not show any message travelling to the server.

这是调用服务的silverlight方法,我将整个方法粘贴为copleteness,lambda是为了使调用同步,我调试了它并且在行client.GetPersonsAsync()之后,Fiddler没有显示任何消息传递给服务器。

    public static List<Person> GetPersonsFromDatabase()
    {
        List<Person> persons = new List<Person>();

        ServiceClient client = new ServiceClient();

        ManualResetEvent eventGetPersons = new ManualResetEvent(false);

        client.GetPersonsCompleted += new EventHandler<GetPersonsCompletedEventArgs>(delegate(object sender, GetPersonsCompletedEventArgs e)
            {
                foreach (DTOperson dtoPerson in e.Result)
                {
                    persons.Add(loadFromDto(dtoPerson));
                }
                eventGetPersons.Set();
            });

        client.GetPersonsAsync();
        eventGetPersons.WaitOne();

        return persons;
    }

Does anyone have any suggestions how I might fix this?

有没有人有任何建议我如何解决这个问题?

3 个解决方案

#1


1  

If the Silverlight application is not hosted in the same domain that exposes the Web service you want to call, then cross-domain restrictions applies.

如果Silverlight应用程序未托管在您要调用的Web服务的同一域中,则适用跨域限制。

If you want the Silverlight application to be hosted in another domain than the web service, you may want to have a look on this post to help you to have a cross domain definition file, or to write a middle "proxy" instead.

如果您希望将Silverlight应用程序托管在除Web服务之外的其他域中,您可能需要查看此帖子以帮助您拥有跨域定义文件,或者编写中间的“代理”。

#2


0  

You wouldn't happen to be running from the filesystem would you? If you are serving up the silverlight application your local machine and not using the VS Web Server or IIS, you won't be able to make HTTP calls for security reasons. Similarly if you're loading from a web server, you can't access local resources.

您不会碰巧从文件系统运行吗?如果您正在为本地计算机提供silverlight应用程序而不使用VS Web服务器或IIS,则出于安全原因,您将无法进行HTTP调用。同样,如果从Web服务器加载,则无法访问本地资源。

Also I've found that Nikhil's Web Development Helper http://www.nikhilk.net/ASPNETDevHelperTool.aspx can be more useful than Fiddler because you will see local traffic as well, although it doesn't look like that is your issue in this case.

另外我发现Nikhil的Web开发助手http://www.nikhilk.net/ASPNETDevHelperTool.aspx比Fiddler更有用,因为你也会看到本地流量,虽然它看起来不像你的问题这个案例。

#3


0  

I am not 100% certain, but if you are running on Vista or Server 2008 you may have run into the User Access Control issue with http.sys

我不是100%肯定,但如果你在Vista或Server 2008上运行,你可能会遇到http.sys的用户访问控制问题

So in Vista and Win2k8 server, the HttpListener will listen only if you are running under a high privelege account. In fact, from my experience, even if you add yourself to the local administrators group, you might run into this issue.

因此,在Vista和Win2k8服务器中,只有在高级别帐户下运行时,HttpListener才会收听。实际上,根据我的经验,即使您将自己添加到本地管理员组,也可能会遇到此问题。

In any case, try launching Visual Studio on Vista by Right Clicking and runas Administrator. See if that fixes it. If it does, you're good, but....

无论如何,尝试通过Right Clicking和runas Administrator在Vista上启动Visual Studio。看看是否能修复它。如果是的话,你很好,但......

ideally you should run httpcfg

理想情况下,你应该运行httpcfg

like: httpcfg set urlacl -u http://itlabws2003 -a D:(A;;GX;;;yoursid)

喜欢:httpcfg set urlacl -u http:// itlabws2003 -a D:(A ;; GX ;;; yoursid)

your sid = the security identifier for the account you're running as, you can find it here: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

你的sid =你正在运行的帐户的安全标识符,你可以在这里找到它:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ ProfileList

if you don't know it already, or you could possibly add yourself to BUILTIN\Administators, find the sid and run the httpcfg via command line again, specifying that sid.

如果你还不知道它,或者你可以将自己添加到BUILTIN \ Administators,找到sid并再次通过命令行运行httpcfg,指定sid。

User Access Control, Vista and Http.sys cause all this...if this is indeed the problem you are running into. Not sure but maybe its worth a try

用户访问控制,Vista和Http.sys导致所有这些......如果这确实是您遇到的问题。不确定,但也许值得一试

#1


1  

If the Silverlight application is not hosted in the same domain that exposes the Web service you want to call, then cross-domain restrictions applies.

如果Silverlight应用程序未托管在您要调用的Web服务的同一域中,则适用跨域限制。

If you want the Silverlight application to be hosted in another domain than the web service, you may want to have a look on this post to help you to have a cross domain definition file, or to write a middle "proxy" instead.

如果您希望将Silverlight应用程序托管在除Web服务之外的其他域中,您可能需要查看此帖子以帮助您拥有跨域定义文件,或者编写中间的“代理”。

#2


0  

You wouldn't happen to be running from the filesystem would you? If you are serving up the silverlight application your local machine and not using the VS Web Server or IIS, you won't be able to make HTTP calls for security reasons. Similarly if you're loading from a web server, you can't access local resources.

您不会碰巧从文件系统运行吗?如果您正在为本地计算机提供silverlight应用程序而不使用VS Web服务器或IIS,则出于安全原因,您将无法进行HTTP调用。同样,如果从Web服务器加载,则无法访问本地资源。

Also I've found that Nikhil's Web Development Helper http://www.nikhilk.net/ASPNETDevHelperTool.aspx can be more useful than Fiddler because you will see local traffic as well, although it doesn't look like that is your issue in this case.

另外我发现Nikhil的Web开发助手http://www.nikhilk.net/ASPNETDevHelperTool.aspx比Fiddler更有用,因为你也会看到本地流量,虽然它看起来不像你的问题这个案例。

#3


0  

I am not 100% certain, but if you are running on Vista or Server 2008 you may have run into the User Access Control issue with http.sys

我不是100%肯定,但如果你在Vista或Server 2008上运行,你可能会遇到http.sys的用户访问控制问题

So in Vista and Win2k8 server, the HttpListener will listen only if you are running under a high privelege account. In fact, from my experience, even if you add yourself to the local administrators group, you might run into this issue.

因此,在Vista和Win2k8服务器中,只有在高级别帐户下运行时,HttpListener才会收听。实际上,根据我的经验,即使您将自己添加到本地管理员组,也可能会遇到此问题。

In any case, try launching Visual Studio on Vista by Right Clicking and runas Administrator. See if that fixes it. If it does, you're good, but....

无论如何,尝试通过Right Clicking和runas Administrator在Vista上启动Visual Studio。看看是否能修复它。如果是的话,你很好,但......

ideally you should run httpcfg

理想情况下,你应该运行httpcfg

like: httpcfg set urlacl -u http://itlabws2003 -a D:(A;;GX;;;yoursid)

喜欢:httpcfg set urlacl -u http:// itlabws2003 -a D:(A ;; GX ;;; yoursid)

your sid = the security identifier for the account you're running as, you can find it here: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

你的sid =你正在运行的帐户的安全标识符,你可以在这里找到它:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ ProfileList

if you don't know it already, or you could possibly add yourself to BUILTIN\Administators, find the sid and run the httpcfg via command line again, specifying that sid.

如果你还不知道它,或者你可以将自己添加到BUILTIN \ Administators,找到sid并再次通过命令行运行httpcfg,指定sid。

User Access Control, Vista and Http.sys cause all this...if this is indeed the problem you are running into. Not sure but maybe its worth a try

用户访问控制,Vista和Http.sys导致所有这些......如果这确实是您遇到的问题。不确定,但也许值得一试