【问题标题】:silverlight is calling WCF service but not passing parameterssilverlight 正在调用 WCF 服务但未传递参数
【发布时间】:2014-01-05 05:14:49
【问题描述】:

我在具有以下设置的项目中有 WCF 服务

//接口

namespace AttendanceSystem
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        void DoWork(string s);
    }
}

//和下面的实现

namespace AttendanceSystem
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public void DoWork(string s)
        {
            StreamWriter file = new StreamWriter(@"C:\users\waqasjafri\desktop\test.txt");
            if (s == null)
            {
                file.WriteLine("value is null");
            }
            else
            {
                file.WriteLine("value is not null");
            }
            file.Close();
        }
    }
}

这是我的解决方案的网站应用程序部分中与 wcf 服务相关的 web.config 文件

  <system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

以及 silverlight 项目中的 ServiceReference.clientconfig 文件

<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647"
                maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:48886/Service1.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
            name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

//这里是调用代码......它是在一个按钮点击触发的事件中

ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
obj.DoWorkAsync("Test");

参数始终作为 null 传递。出了什么问题?我无法弄清楚,因为我是集成 WCF/silverlight/Asp.net 的新手。

【问题讨论】:

  • 你能把调用代码也贴出来吗?
  • 刚刚做了?你怎么看?
  • 您能否使用 Fiddler 检查您的请求,然后查看请求是否正在发出并包含您的值。如果是这样,那么问题出在服务器上。请发布您的服务器端端点配置详细信息。
  • 好吧,当我在服务接口中创建数据协定并使用类似 ServiceReference1.Item i = new ServiceReference1.Item { val = "Test"};然后将其传递给方法。不知道为什么字符串没有在相同的设置下通过。

标签: c# asp.net wcf silverlight


【解决方案1】:

再次更新您的服务参考。像这样尝试,在 Silverlight 中,您的方法应该有一个已完成的事件,

  ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            client.DoWorkCompleted += (a, ae) =>
            { 

            };
            client.DoWorkAsync("Test");

【讨论】:

  • 我不认为我们需要完成活动注册。如果不想回调,我们可以简单地调用异步方法。如果我错了,请纠正我。
【解决方案2】:

您可能超出了默认情况下为 8192 的 maxStringContentLength。见这里:

http://msdn.microsoft.com/en-us/library/ms731361(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/ms731325(v=vs.110).aspx

您也可以考虑使用二进制消息编码。

【讨论】:

  • 他的字符串长度只有 4 个字符,因为他将“Test”作为参数传递。所以我不认为这是一个问题。
猜你喜欢
  • 2015-12-31
  • 2018-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
  • 2016-03-30
  • 1970-01-01
相关资源
最近更新 更多