GVKun编程网logo

c# – 如何为现有的WCF服务本机启用JSONP?(c#调用wcf服务接口)

17

本文的目的是介绍c#–如何为现有的WCF服务本机启用JSONP?的详细情况,特别关注c#调用wcf服务接口的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解c#–如何为

本文的目的是介绍c# – 如何为现有的WCF服务本机启用JSONP?的详细情况,特别关注c#调用wcf服务接口的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解c# – 如何为现有的WCF服务本机启用JSONP?的机会,同时也不会遗漏关于#翻译# 将基于SOAP的WCF服务转成RESTful设计、asp.net – WCF数据服务还是只是WCF服务?、asp.net – 从启用AJAX的WCF服务返回错误详细信息、asp.net – 如何为WCF服务创建重定向?的知识。

本文目录一览:

c# – 如何为现有的WCF服务本机启用JSONP?(c#调用wcf服务接口)

c# – 如何为现有的WCF服务本机启用JSONP?(c#调用wcf服务接口)

我有一个现有的服务,如下面的方法:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class SomeService : ISomething
{
    public SomeListResults SomeList(SomeParams someParams)
    {
          ....
    }
}

是否有一种简单的方法可以同时允许JSONP调用和JSON(检测它).这是原生的吗?

解决方法

将配置更新为:
<configuration>
  <system.web>
    <compilation debug="true" targetframework="4.0">
    <authentication mode="None">
  </authentication></compilation></system.web>
  <system.webserver>
    <modules runallmanagedmodulesforallrequests="true">
  </modules></system.webserver>
  <system.servicemodel>
    <servicehostingenvironment **aspnetcompatibilityenabled**="true">
    <standardendpoints>
      <webscriptendpoint>
        <standardendpoint **crossdomainscriptaccessenabled**="true" name="">
      </standardendpoint></webscriptendpoint>
    </standardendpoints>
  </servicehostingenvironment></system.servicemodel>
</configuration>

See here for a blog post提供了创建可访问跨域的wcf服务的演练.

这将使您的服务能够接受来自跨域源的请求.

在确定是否填充您的响应(jsonp中的p)方面,

感谢@carlosfigueira:

If using .Net 4 JSONP is supported natively. As long as the request has a query string parameter called “callback” (this name can be configured),the response will be padded with the function name
.

否则,您需要编写一个自定义消息检查器,以适当地填充响应.

#翻译# 将基于SOAP的WCF服务转成RESTful设计

#翻译# 将基于SOAP的WCF服务转成RESTful设计

当SOAP服务被大量的使用的时候, 一些开发者可能就会选用 RESTful services. 当开发者需要进行大范围的交互并且仅被限于使用基于HTTP协议传输的XML信息或JSON格式的信息时,通常会用到REST (Representative State Transfer) 。RESTful 服务与基于SOAP的服务是不同的,因为他们不能尝试的去达到中立性的传输形式。事实上, RESTful服务通常是绑定HTTP协议作为唯一的传输形式在整个系统中去使用的。 使用REST, 开发者能够模仿它们的服务作为一种资源,并且在URI指定的Form表单中给予这些资源唯一的标识。 在该文章中,我们将会把当前现有的基于SOAP协议的服务,转换成为一种更好的RESTful的设计实现。

asp.net – WCF数据服务还是只是WCF服务?

asp.net – WCF数据服务还是只是WCF服务?

我想决定走哪条路.我有一个解决方案,需要一个Web服务和一个客户端,这是一个 Windows Phone 7项目. WP7项目需要通过WCF服务与数据库通信.

关于我应该选择哪种方式,以及常规WCF服务文件VS WCF数据服务的区别,优点/缺点,我有点困惑.
考虑到我的wp7应用程序需要在数据库上的某些表上运行查询,哪种方式会更容易.

任何解释都会受到欢迎.
谢谢

解决方法

如果您需要CRUD和灵活的查询功能,WCF数据服务非常棒 – 它们允许您以最少的开发工作来公开底层数据(例如通过实体框架)和控制安全性,作为RESTful API,特别是对于AJAX和SPA类型的客户端前端结束. (另请注意,WebAPI现在也提供类似的功能).

WCF服务更多用于正式的“服务”和“运营”集成功能,其中有更多的业务重点,例如规则,处理,工作流程等
例如WCF对提交处理索赔(数据输入和输出的自定义/丰富图形),触发每夜批处理作业(无效响应)等非常有用.

此外,您可以结合使用这两种技术,例如对于CQR​​S类型体系结构,通过使用数据服务进行查询,使用WCF进行命令类型功能.

asp.net – 从启用AJAX的WCF服务返回错误详细信息

asp.net – 从启用AJAX的WCF服务返回错误详细信息

简短版本:在 AJAX启用的WCF服务中抛出异常(除了打开门并发回所有异常详细信息之外),是否有/有什么建议的方式将错误详细信息返回给客户端?

长版本:

我有一个相对简单的启用AJAX的WCF服务,我使用默认服务代理从客户端调用.我在下面提供了代码片段,但我不相信代码本身有什么问题.

我的问题是,如果我在服务中抛出异常,返回给客户端的错误对象总是通用的:

{
    "ExceptionDetail":null,"ExceptionType":null,"Message":"The server was unable to process the request..."
    "StackTrace":null
}

理想情况下,我想在客户端上显示不同的错误消息,具体取决于出错.

一个选择是允许在WCF故障中的异常,这将为我提供完整的堆栈跟踪和一切,但我感谢安全性的关注,这实际上比我需要更多的信息.我可以做的只是能够发回一个描述问题或某事的字符串,但我没有看到一种方法来做到这一点.

我的服务代码:

[ServiceContract(Namespace = "MyNamespace")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
    [OperationContract]
    public void DoStuff(string param1,string etc)
    {
        //Do some stuff that maybe causes an exception
    }
}

在客户端上

MyNamespace.MyService.DoStuff(
    param1,etc,function() { alert("success"); },HandleError);

其中“HandleError”只是一个通用错误处理方法,将显示有关错误的详细信息.

解决方法

编辑:用适当的自定义json错误处理程序更新了该帖子

快速但非优先的方式.

<serviceDebug includeExceptionDetailInFaults="true"/>

在您的服务行为将给您所需的所有细节.

好的方式

应用程序的所有异常都将转换为JsonError并使用DataContractJsonSerializer进行序列化. Exception.Message直接使用. FaultExceptions提供了FaultCode,其他异常处理为未知的faultcode -1.

使用HTTP状态代码400发送FaultException,其他异常是HTTP代码500 – 内部服务器错误.这不是必需的,因为faultcode可以用来决定是否和未知的错误.这在我的应用程序是方便的

错误处理程序

internal class CustomErrorHandler : IErrorHandler
{
    public bool HandleError(Exception error)
    {
        //Tell the system that we handle all errors here.
        return true;
    }

    public void ProvideFault(Exception error,System.ServiceModel.Channels.MessageVersion version,ref System.ServiceModel.Channels.Message fault)
    {
        if (error is FaultException<int>)
        {
            FaultException<int> fe = (FaultException<int>)error;

            //Detail for the returned value
            int faultCode = fe.Detail;
            string cause = fe.Message;

            //The json serializable object
            JsonError msErrObject = new JsonError { Message = cause,FaultCode = faultCode };

            //The fault to be returned
            fault = Message.CreateMessage(version,"",msErrObject,new DataContractJsonSerializer(msErrObject.GetType()));

            // tell WCF to use JSON encoding rather than default XML
            WebBodyFormatMessageProperty wbf = new WebBodyFormatMessageProperty(WebContentFormat.Json);

            // Add the formatter to the fault
            fault.Properties.Add(WebBodyFormatMessageProperty.Name,wbf);

            //Modify response
            HttpResponseMessageProperty rmp = new HttpResponseMessageproperty();

            // return custom error code,400.
            rmp.StatusCode = System.Net.HttpStatusCode.BadRequest;
            rmp.StatusDescription = "Bad request";

            //Mark the jsonerror and json content
            rmp.Headers[HttpResponseHeader.ContentType] = "application/json";
            rmp.Headers["jsonerror"] = "true";

            //Add to fault
            fault.Properties.Add(HttpResponseMessageProperty.Name,rmp);
        }
        else
        {
            //Arbitraty error
            JsonError msErrObject = new JsonError { Message = error.Message,FaultCode = -1};

            // create a fault message containing our FaultContract object
            fault = Message.CreateMessage(version,new DataContractJsonSerializer(msErrObject.GetType()));

            // tell WCF to use JSON encoding rather than default XML
            var wbf = new WebBodyFormatMessageProperty(WebContentFormat.Json);
            fault.Properties.Add(WebBodyFormatMessageProperty.Name,wbf);

            //Modify response
            var rmp = new HttpResponseMessageproperty();

            rmp.Headers[HttpResponseHeader.ContentType] = "application/json";
            rmp.Headers["jsonerror"] = "true";

            //Internal server error with exception mesasage as status.
            rmp.StatusCode = System.Net.HttpStatusCode.InternalServerError;
            rmp.StatusDescription = error.Message;

            fault.Properties.Add(HttpResponseMessageProperty.Name,rmp);
        }
    }

    #endregion
}

WebbehavIoUr用于安装上述错误处理程序

internal class AddErrorHandlerBehavior : WebHttpBehavior
{
    protected override void AddServerErrorHandlers(ServiceEndpoint endpoint,Endpointdispatcher endpointdispatcher)
    {
        base.AddServerErrorHandlers(endpoint,endpointdispatcher);

        //Remove all other error handlers
        endpointdispatcher.Channeldispatcher.ErrorHandlers.Clear();
        //Add our own
        endpointdispatcher.Channeldispatcher.ErrorHandlers.Add(new CustomErrorHandler());
    }
}

json错误数据合同

指定json错误格式.
在此添加属性以更改错误格式.

[DataContractFormat]
public class JsonError
{
    [DataMember]
    public string Message { get; set; }

    [DataMember]
    public int FaultCode { get; set; }
}

使用错误处理程序

自托管

ServiceHost wsHost = new ServiceHost(new Webservice1(),new Uri("http://localhost/json")); 

ServiceEndpoint wsEndpoint = wsHost.AddServiceEndpoint(typeof(IWebservice1),new WebHttpBinding(),string.Empty);

wsEndpoint.Behaviors.Add(new AddErrorHandlerBehavior());

App.config中

<extensions>  
  <behaviorExtensions>  
    <add name="errorHandler"  
        type="WcfServiceLibrary1.ErrorHandlerElement,WcfServiceLibrary1" />  
  </behaviorExtensions>  
</extensions>

asp.net – 如何为WCF服务创建重定向?

asp.net – 如何为WCF服务创建重定向?

我在我的网站A上托管了一个WCF服务.我有另一个站点B,它使用IIS URL重写将所有请求重定向到我的站点A.但是,站点B不处理对.svc文件的任何请求,返回404未找到.知道如何让它工作吗?

UPD重定向配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect" stopProcessing="true">
                    <match url=".*" />
                    <action type="Rewrite" url="http://localhost/site_A/{R:0}" logRewrittenUrl="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

我发现网站A(!)的IIS日志中没有对.svc文件的任何请求.它不会将这些请求重定向到站点A.但是,当我从站点B请求.htm文件时,它会正确地将它们重定向到站点A.

解决方法

我不确定,但据我记得,如果您在网站B上没有.svc的http处理程序,可能会发生这种情况.

我们今天的关于c# – 如何为现有的WCF服务本机启用JSONP?c#调用wcf服务接口的分享就到这里,谢谢您的阅读,如果想了解更多关于#翻译# 将基于SOAP的WCF服务转成RESTful设计、asp.net – WCF数据服务还是只是WCF服务?、asp.net – 从启用AJAX的WCF服务返回错误详细信息、asp.net – 如何为WCF服务创建重定向?的相关信息,可以在本站进行搜索。

本文标签: