How to achieve high-performance REST API on Azure with .NET?

后端 未结 7 1669
Happy的楠姐
Happy的楠姐 2021-02-07 15:24

We have a .NET Web Role hosted on Windows Azure that only serves a REST API with only a hand few web methods.

API is u

7条回答
  •  故里飘歌
    2021-02-07 15:40

    Is the size of the messages your service is receiving so big because there is a large amount of data in the message or because they contain files?

    If it is the first case, then ProtoBuf does indeed seem like a very good option.

    If the message size is big because it embeds files, then one strategy I have been using with success is creating two different architectures for your service methods: one for methods that upload and download files and another one for methods that only send and receive messages.

    The file related methods will simply transmit the files inside the body of the HTTP requests, in binary form without any transformation or encoding. The rest of the parameters will be send using the request URL.

    For file uploads, in WCF REST services, in the service method you will have to declare the parameter representing the file of being of type Stream. For example:

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "uploadProjectDocument?projectId={projectId}")]
    void UploadProjectDocument(Guid projectId, Stream document);
    

    When encountering Stream parameters, WCF will simply take their content directly from the body of the request without doing any processing on it. You can only have one parameter of type Stream on a service method (which makes sense because each HTTP request has only one body).

    The downside to the above approach is that besides the parameter representing the file, all the other ones need to be of basic types (like strings, number, GUIDs). You cannot pass any complex object. If you need to do that you will have to create a separate method for it, so you might end up having two methods (which will translate in two calls at runtime) where at the moment you have only one. However, uploading files directly in the body of the request should be much more efficient than serializing them, so even with the extra call things should be improved.

    For downloading files from the service you will need to declare the WCF methods as returning Stream and simply write the file in returned object. As with the Stream paramters, WCF will output the content of the Stream directly into the body of the result without any transformations on it.

提交回复
热议问题