【问题标题】:where the client proxy be saved保存客户端代理的位置
【发布时间】:2012-07-13 17:50:15
【问题描述】:

我添加了对 WCF 服务的 Web 引用

我想修改代理文件由web服务参考工具自动生成

我想在每个方法之前添加类似的属性

[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,

BodyStyle = WebMessageBodyStyle.Wrapped,

UriTemplate = "LogIn/{username}/{password}")]

知道怎么做

最好的问候

【问题讨论】:

  • 我认为设置是在 web.config 文件中添加的。

标签: asp.net wcf


【解决方案1】:

更新答案: 看来您已经使用 VS 中的“添加服务引用”对话框创建了代理代码。 VS ASR 对话框不完全支持 WCF REST,因此代理代码缺少 [WebInvoke] 属性。您可以尝试在客户端代理中的操作上添加[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)] 属性吗? 资源: WCF Service Proxy throws exception when more than one parameter is used in [OperationContract] method

如果我清楚地理解您的问题。 如果您想在每个方法之前添加 WebInvoke 属性,请转到您的 WCF 服务接口类,例如 ( IService.cs )。可能是放在您的 App_Code 文件夹中。

[ServiceContract]
public interface IService
{ 
 [OperationContract]
 [WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped,UriTemplate = "LogIn/{username}/{password}")]
void DoWork();

}

如果您想更改代理类设置,请转到 system.ServiceModel 标签下的 Web 配置。

 <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:56662/WebSite2/Service.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
            contract="ServiceReference1.IService" name="BasicHttpBinding_IService" />
    </client>
    <behaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

【讨论】:

  • @AMH 检查我更新的答案,确定它会起作用,并记得将其标记为您的答案。
  • localhost:1381/PMAHost/Service.svc 上没有可以接受消息的端点监听。这通常是由不正确的地址或 SOAP 操作引起的。有关更多详细信息,请参阅 InnerException(如果存在)。
  • 我的端点看起来像 localhost:1381/PMAHost/Service.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference.IServices" name="BasicHttpBinding_IService" />
  • 我仍然无法在我的网站上使用它
【解决方案2】:

你好:

我之所以添加这个,是因为上面的帖子并没有明确说明如何在客户端代理代码中添加 WebInvoke 属性,所以我提供这个是为了消除关于它是如何完成的任何困惑。

步骤 1

转到您的 VS 项目文件夹并找到您的“服务引用”文件夹。找到您正在使用的参考文件夹,如果您在添加服务时使用了默认文件夹,那么应该类似于“ServiceReference1”。

在该文件夹中有一个 Reference.cs 文件,这是您更新方法所需的文件。

例如,假设您创建了一个 REST 服务并且您的 WebInvoke 属性被忽略了。你可以在那里添加它们。

第 2 步

只要找到以“[System.ServiceModel.OperationContractAttribute”开头的那一行:

应该是这样的

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/YourMethodName", ReplyAction="http://tempuri.org/IService1/YourMethodNameResponse")]

第三步

现在,您想在该行之后添加您的 WebInvoke 代码,如下所示:

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/YourMethodName", ReplyAction="http://tempuri.org/IService1/YourMethodNameResponse")]

[System.ServiceModel.Web.WebInvoke(Method = "POST", BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.WrappedRequest, RequestFormat = System.ServiceModel.Web.WebMessageFormat.Json, ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, UriTemplate = "/YourMethodName")]

这就是它的全部内容。

注意:如果您删除了 WCF 服务引用,您所做的更改将会消失。因此,请确保在完成更改后备份文件。

【讨论】:

    猜你喜欢
    • 2016-07-19
    • 2012-05-12
    • 1970-01-01
    • 2022-01-07
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 2020-09-19
    相关资源
    最近更新 更多