简体   繁体   English

为Asp.Net Web服务添加服务参考

[英]Adding service reference for Asp.Net webservice

EDIT: 编辑:
I'm based on some comments that have been deleted, I guess I need to add a service reference and create a instance of the webservice object in the client application to pass the dataset. 我基于已删除的一些注释,我想我需要添加服务引用并在客户端应用程序中创建webservice对象的实例以传递数据集。 I haven't been able to add the service reference. 我无法添加服务参考。 When the service runs, it gives a 403 forbidden error, and has done so since I first created the project. 服务运行时,它会给出403禁止的错误,并且自从我第一次创建项目以来就这样做了。 I thought this was fine because the apicontroller methods can still be accessed with an HTTP GET, but I have no way of actually sending objects to the service, and can't create the service reference. 我认为这很好,因为仍然可以使用HTTP GET访问apicontroller方法,但是我无法将对象实际发送到服务,也无法创建服务引用。 The service and the client are in separate projects. 服务和客户端位于单独的项目中。 At this point even a link to a decent tutorial would help. 在这一点上,即使是一个体面的教程的链接也会有所帮助。 I've found a ton of stuff for creating a full site with a few functions accessed in jquery, and plenty of stuff that includes how to send a post in C#, but nothing that combines both sides. 我发现了大量用于创建完整网站的东西,其中包含在jquery中访问的一些功能,还有很多东西,其中包括如何使用C#发送帖子,但没有将两者结合在一起的东西。 I saw this question asking a how to do a similar thing. 我看到了这个问题,问如何做类似的事情。 Is there any difference in the client side code I have in c# and the javascript in the question? 我在c#和问题中的javascript中的客户端代码有什么区别吗?

EDIT2: I tried using a memory stream to look at the request content to see if the entire dataset was being transferred. EDIT2:我尝试使用内存流查看请求内容,以查看是否正在传输整个数据集。 Its only picking up 4000 or so bytes out of 350000 being transferred. 它仅从350000个字节中提取4000个左右的字节进行传输。 I assume this is why the dataset isn't availalbe in teh webservice parameter. 我假设这就是为什么该数据集在webservice参数中不可用的原因。 but I'm not sure how to continue. 但我不确定如何继续。

Original: 原版的:

I'm building a webservice to handle the database calls for my client application. 我正在构建一个Web服务来处理我的客户端应用程序的数据库调用。 This isn't publicly facing, so I'm fine with passing a dataset instead of a custom model. 这不是公开的,所以我可以传递数据集而不是自定义模型。 Heres the client code: 这是客户端代码:

public static void TestSendToDatabase(DataSet Products)
{
        string xml = ToXML(Products);
        byte[] bytes;
        bytes = System.Text.Encoding.ASCII.GetBytes(xml);

        StringBuilder urlBuilder = new StringBuilder(string.Format("{0}{1}", BaseUrl, "WalmartProducts/Update"));
        string url = urlBuilder.ToString();

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Accept = "application/xml";
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = bytes.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(bytes,0,bytes.Length);

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

And the service code: 以及服务代码:

public class WalmartProductsController : ApiController
{
    [HttpPost]
    [ActionName("Update")]
    public object UpdateWalmartProducts([FromBody] byte[] bytes)
    {
        string xml = System.Text.Encoding.ASCII.GetString(bytes);
        DataSet productsFromClient = LoadFromXMLString(xml);
        DataUpdater.UpdateWalmartData(productsFromClient.Tables[0]);
        return DataUpdater.GetProducts();
    }

The byte array is empty in the web service. 字节数组在Web服务中为空。 I'm sure that I'm mixing methods that shouldn't be mixed, but I've gone through at least thirty articles trying to resolve this, and it feels like each one has a completely different method. 我确定我正在混合不应混合使用的方法,但是我已经阅读了至少三十篇文章试图解决此问题,感觉每个人都有完全不同的方法。 The webservice is currently returning the dataset in an xml format, so I would prefer to stick with xml over json. Web服务当前以xml格式返回数据集,因此我宁愿在json上坚持使用xml。

This is not a complete answer, but perhaps it is a starting point. 这不是一个完整的答案,但是也许这是一个起点。 .NET supports two main techniques for writing Web Services. .NET支持两种主要的Web服务编写技术。 The first is an older technique using .asmx files. 第一种是使用.asmx文件的较旧技术。 This approach is generally called the SOAP approach, and Visual Studio generates signatures of your methods in a .wsdl file. 这种方法通常称为SOAP方法,Visual Studio在.wsdl文件中生成方法的签名。 Because the signatures are exposed, you can then ask Visual Studio to create a Web Reference file for you. 由于签名是公开的,因此您可以要求Visual Studio为您创建Web参考文件。 This web reference exposes all the Web Services as simple classes and methods, hiding all the complexity of the communication across the web service. 该Web参考将所有Web服务公开为简单的类和方法,隐藏了整个Web服务之间通信的所有复杂性。 Any object that can be serialized, including the DataSet object, can be included as a parameter to these web methods, and .NET will take care of serializing and deserializing these objects as they pass across the HTTP boundary. 可以将可以序列化的任何对象(包括DataSet对象)作为这些Web方法的参数,并且.NET将在它们通过HTTP边界时对这些对象进行序列化和反序列化。 I believe that MS then encapsulated a number of different communication technologies inside of a product they called WCF, which was supposed to allow you to use configuration to choose whether you wanted to use HTTP for communication or TCP/IP. 我相信MS然后将许多不同的通信技术封装在一个称为WCF的产品中,该产品应该允许您使用配置来选择要使用HTTP进行通信还是使用TCP / IP。

The downside to this approach is that if you use any MS class in your API's, you have effectively tied your product to MS, and it is then very difficult for any non-MS client to communicate with your web service. 这种方法的缺点是,如果您在API中使用任何MS类,则您已将产品有效地绑定到了MS,因此,任何非MS客户端都很难与Web服务进行通信。 The DataSet object is an extremely complicated one, and I'm under the opinion that it would be quite impractical to attempt to write your own serialization/deserialization methods to send a DataSet . DataSet对象是一个非常复杂的对象,我认为尝试编写自己的序列化/反序列化方法来发送DataSet是非常不切实际的。

The other Web Service approach that is gaining popularity is using REST API's. 另一种流行的Web服务方法是使用REST API。 Recently, MS added support for these, and the ApiController is one of the starting points. 最近,MS添加了对这些功能的支持,而ApiController是其中的起点之一。 If you google "ASP.NET REST API Tutorial" you should find all kinds of examples. 如果您使用Google“ ASP.NET REST API教程”,则应找到各种示例。 However, with this approach I would discourage you from using the DataSet class. 但是,通过这种方法,我不鼓励您使用DataSet类。 I've had some success with classes such as a Dictionary, provided the object classes you put into the dictionary all resolve down to fairly simple components. 我对诸如Dictionary之类的类取得了一些成功,但前提是您放入字典中的对象类都可以分解为相当简单的组件。 Attempting to use a complex MS class like DataSet is likely to lead to a lot of frustration, and will yield a Web Service that very few clients can use. 尝试使用诸如DataSet之类的复杂MS类可能会导致很多挫败感,并且将产生很少客户端可以使用的Web Service。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM