asp.net web api: accessing uploaded file stream - asp.net

I have an asp.net web api application which is acting as a Relay to a wcf web service. In certain scenarios I want to upload large files. The methods in the wcf service accept files as stream.
I do not want to save the files on my intermediate server I want to access the stream of the uploaded file and provide it to the wcf method so that the data is directly streamed to the wcf service.
Here is similar scenario when client is downloading the file
using (IProductsChannel channel = ChannelFactory.CreateChannel())
{
result.Content = new StreamContent(channel.GetFile());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
return result;
}

Here is at least one way of doing it. using the HTTPContext the only problem with this one is that it is not good for unit testing so we have to abstract it out in the final solution.
var file = HttpContext.Current.Request.Files[0];
var result = new HttpResponseMessage(HttpStatusCode.OK);
using (IProductsChannel channel = ChannelFactory.CreateChannel())
{
channel.SaveFile(file.InputStream);
}
return new HttpResponseMessage(HttpStatusCode.Created);
}

Related

Using an asp web api in wpf

So i have got a simple question, when using our cms we can attach a driver as an executable.
The driver we want to make is an httpreceiver or just an api endpoint. SO i tought lets use asp.net web api for it -> using version .net 4.6.1. altough asp.net application requires a webserver and is not an executable, But i read on google you can use it inside a wpf application since our cms is wpf in the first place.
So my question is is there a way i can use my mvc web api project inside a wpf application? and if not what would be the best bet to have an httpreceiver or httppost receiver into an executable?
Main reason is we want to send httppost requests to the server as a desktop application. I know it's complicated but thats how it needs to be as far as I know.
In the case where asp is not an option, what the best way to make a postreqst/ httpreceiver as a desktop application?
EDit:
the resource guide from microsoft beneath was perfectly however i still have a question:
string baseAddress = "http://localhost:9000/";
// Start OWIN host
using (WebApp.Start<Startup>(url: baseAddress))
{
// Create HttpClient and make a request to api/values
HttpClient client = new HttpClient();
string username = "test".ToUpper().Trim();
string password = "test123";
//Mock data
var body = new PostTemplate1();
body.Description = "test";
body.StateDesc = "httpdriver/username";
body.TimeStamp = DateTime.Now;
body.Message = "This is a post test";
var json = JsonConvert.SerializeObject(body);
var data = new StringContent(json, Encoding.UTF8, "application/json");
var authToken = Encoding.ASCII.GetBytes($"{username}:{password}");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));
var response = await client.PostAsync(baseAddress + #"api/Post", data);
var result = response.StatusCode;
}
As the guide says you post to url with port 9000
is there a possibility to use another port and use https?
if yes where to manage certificates for handling https?

Using WebClient to get a intranet files

In our I have company intranet a server, that is responsible for storing files. Initially, the server had to operate only in an intranet environment, but now there is a need to share files with external web applications. Making this server accessible from the internet is not an option.
I want to create a ASP.NET MVC solution that uses the WebClient to get these files from the intranet server and send back them to the user through FileResult of the external app. This client would be provided with custom domain user credentials. So far I have tried to create a CredentialCache class, set correct credentials and append it to WebClients Credentials property like in the following code:
public ActionResult Download(int id, string fileName)
{
var fileService = new FilesService();
var documentUrl = fileService.GetUrlFileByFileId(id);
string filePath = "http://my.intranet.com/" + documentUrl;
var fileNameFromUrl = filePath.Substring(filePath.LastIndexOf("\\") + 1);
byte[] filedata;
CredentialCache cc = new CredentialCache();
cc.Add(new Uri("http://my.intranet.com/"),
"ntlm",
new NetworkCredential("myUserName", "myPassword", "myDomain"));
using (var client = new WebClient())
{
client.Credentials = cc;
filedata = client.DownloadData(filePath);
}
string contentType = MimeMapping.GetMimeMapping(filePath);
var cd = new ContentDisposition
{
FileName = fileName,
Inline = false
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(filedata, contentType);
}
According to the question posted in Domain credentials for a WebClient class don't work it should work, but it’s not. It’s running only if I run the problem on localhost, but when I publish my solution on a test server, it return 401 error. My question is did how to get this working? And is it possible to download files through this method?
UPDATE--- I've published my test app on another server and it started to working. Now the test app is on another server than the server That stores files. Any ideas why it's not working when both are on the same machine?
401 error is unauthorized, so perhaps the issue is related to permissions. Are you sure the user account you are using to login to that folder has the proper access?
Ok, I found the solution on this site: https://blogs.msdn.microsoft.com/distributedservices/2009/11/10/wcf-calling-wcf-service-hosted-in-iis-on-the-same-machine-as-client-throws-authentication-error/
The solution was to add an registry entry and add my web apps to this entry to allow back connections.

Publishing with Core Service and Impersonation

I have a Tridion Core Service Web Application to publish pages. When logged into the server and running it from there via a browser client calling a web service with ajax it works fine. However, when I run the application from my desktop it does nothing, and also throws no error messages.
*Edit:
The Web App hosting the web service is running as an 'Application' under the Tridion 2011 CMS website. This is done to avoid cross-domain ajax issues/
Update: The code below is working fine - both with the impersonate and also with Nick's solution. My issue was actually in how I was calling the web service from jQuery and using the appropriate URL. I am leaving the code and question so maybe it will help others.
My code is:
string binding = "wsHttp_2011";
using (var client = new SessionAwareCoreServiceClient(binding))
{
client.Impersonate("company\\cms_svc");
// ** Get Items to Publish
List<string> itemsToPublish = GetItemsToPublish(publishItem.TcmUri, client);
PublishInstructionData instruction = new PublishInstructionData
{
ResolveInstruction = new ResolveInstructionData() { IncludeChildPublications = false },
RenderInstruction = new RenderInstructionData()
};
PublicationTargetData pubtarget = (PublicationTargetData)client.Read(publishItem.PubTargetUri, readoptions);
List<string> target = new List<string>();
target.Add(pubtarget.Id);
client.Publish(itemsToPublish.ToArray(), instruction, target.ToArray(), GetPublishPriority(publishItem.Priority), readoptions);
}
Have at look at this page on SDL Live Content, which explains various types of scenarios for connecting as different users:
http://sdllivecontent.sdl.com/LiveContent/content/en-US/SDL_Tridion_2011_SPONE/task_87284697A4BB423AAD5387BBD6884735
As per the docs, instead of impersonation you may want to establish your Core Service connection as follows using NetworkCredential:
using (ChannelFactory<ISessionAwareCoreService> factory =
new ChannelFactory<ISessionAwareCoreService>("netTcp_2011"))
{
NetworkCredential networkCredential =
new NetworkCredential("username", "password", "domain");
factory.Credentials.Windows.ClientCredential = networkCredential;
ISessionAwareCoreService client = factory.CreateChannel();
Console.WriteLine(client.GetCurrentUser().Title);
}

WCF Client Proxies, Client/Channel Caching in ASP.Net - Code Review

long time ASP.Net interface developer being asked to learn WCF, looking for some education on more architecture related fronts - as its not my strong suit but I'm having to deal.
In our current ASMX world we adopted a model of creating ServiceManager static classes for our interaction with web services. We're starting to migrate to WCF, attempting to follow the same model. At first I was dealing with performance problems, but I've tweaked a bit and we're running smoothly now, but I'm questioning my tactics. Here's a simplified version (removed error handling, caching, object manipulation, etc.) of what we're doing:
public static class ContentManager
{
private static StoryManagerClient _clientProxy = null;
const string _contentServiceResourceCode = "StorySvc";
// FOR CACHING
const int _getStoriesTTL = 300;
private static Dictionary<string, GetStoriesCacheItem> _getStoriesCache = new Dictionary<string, GetStoriesCacheItem>();
private static ReaderWriterLockSlim _cacheLockStories = new ReaderWriterLockSlim();
public static Story[] GetStories(string categoryGuid)
{
// OMITTED - if category is cached and not expired, return from cache
// get endpoint address from FinderClient (ResourceManagement SVC)
UrlResource ur = FinderClient.GetUrlResource(_contentServiceResourceCode);
// Get proxy
StoryManagerClient svc = GetStoryServiceClient(ur.Url);
// create request params
GetStoriesRequest request = new GetStoriesRequest{}; // SIMPLIFIED
Manifest manifest = new Manifest{}; // SIMPLIFIED
// execute GetStories at WCF service
try
{
GetStoriesResponse response = svc.GetStories(manifest, request);
}
catch (Exception)
{
if (svc.State == CommunicationState.Faulted)
{
svc.Abort();
}
throw;
}
// OMITTED - do stuff with response, cache if needed
// return....
}
internal static StoryManagerClient GetStoryServiceClient(string endpointAddress)
{
if (_clientProxy == null)
_clientProxy = new StoryManagerClient(GetServiceBinding(_contentServiceResourceCode), new EndpointAddress(endpointAddress));
return _clientProxy;
}
public static Binding GetServiceBinding(string bindingSettingName)
{
// uses Finder service to load a binding object - our alternative to definition in web.config
}
public static void PreloadContentServiceClient()
{
// get finder location
UrlResource ur = FinderClient.GetUrlResource(_contentServiceResourceCode);
// preload proxy
GetStoryServiceClient(ur.Url);
}
}
We're running smoothly now with round-trip calls completing in the 100ms range. Creating the PreloadContentServiceClient() method and adding to our global.asax got that "first call" performance down to that same level. And you might want to know we're using the DataContractSerializer, and the "Add Service Reference" method.
I've done a lot of reading on static classes, singletons, shared data contract assemblies, how to use the ChannelFactory pattern and a whole bunch of other things that I could do to our usage model...admittedly, some of its gone over my head. And, like I said, we seem to be running smoothly. I know I'm not seeing the big picture, though. Can someone tell me what I've ended up here with regards to channel pooling, proxy failures, etc. and why I should head down the ChannelFactory path? My gut says to just do it, but my head can't comprehend why...
Thanks!
ChannelFactory is typically used when you aren't using Add Service Reference - you have the contract via a shared assembly not generated via a WSDL. Add Service Reference uses ClientBase which is essentially creating the WCF channel for you behind the scenes.
When you are dealing with REST-ful services, WebChannelFactory provides a service-client like interface based off the shared assembly contract. You can't use Add Service Reference if your service only supports a REST-ful endpoint binding.
The only difference to you is preference - do you need full access the channel for custom behaviors, bindings, etc. or does Add Service Reference + SOAP supply you with enough of an interface for your needs.

Upload Large Files from ASP .Net Application

I am building a website where i need a page where user can upload large video files, i have created WCF service with streaming but i am calling that WCF service from Button_Click event of web page.
I have used below mentioned article for WCF service creation
WCF Streaming
I have used streaming as it should be efficient and should not be buffered in memory of server.
Now questions
1) I am having doubts that the entire file is uploaded to the web server and then it is transferred to WCF Service server...if this is true then i am not getting advantage of streaming as well as iis and web server will be down very soon if user uploads large file or multiple user are uploading files con currently
2) Is there any other efficient way to do same operation with some other technique
Please help me ...
EDIT :
If I am not calling WCF Service method from ASP .Net code in that case also it is transferring bytes to the web server which i have checked with HTTPFox
I have checked above thing with upload control and putting one button on UI whose click event is bound to one method in code behind.
So, still i am having that confusion that how data is transferred
Client Machine - Web Server (ASP .Net Application) - Service Server (WCF Service)
Client Machine - Service Server (WCF Service)
NOTE : If i am putting a debug point on button_click and uploading 10 kb file it hits that in less then 1 sec. but if i am uploading 50 mb file then it is taking time.
I placed code of calling WCF service inside that button_click event
1) I am having doubts that the entire
file is uploaded to the web server and
then it is transferred to WCF Service
server...if this is true then i am not
getting advantage of streaming as well
as iis and web server will be down
very soon if user uploads large file
or multiple user are uploading files
con currently
No, you're confusing stuff here. When you use WCF streaming to upload a large file, the file is being sent in chunks - in blocks of several Kbyte in size. The WCF server - running in IIS or self-hosted in a NT service or a console app - while receive those chunks and write them to disk, as they arrive.
You don't "upload the whole file to the web server" and then "transfer it" to the WCF service - the WCF service itself is receiving and handling the file - and only once.
If you host your WCF service yourself - in a console app, a Winforms app, or a Windows NT Service - there's not even any IIS or web server involved AT ALL. WCF handles it all by itself.
Using WCF streaming is probably one of the most memory efficient and one of the simplest ways to transfer large files to a server.
Check out some more example and blog posts on the topic:
MSDN WCF Streaming Sample
Data Transfer Using Self Hosted WCF Service
Sending Attachments with WCF
Progress Indication while Uploading/Downloading Files using WCF
Here is your best solution, I went the same route as you and concluded ftp is easier and works flawlessly. Here is some example code:
First get this library, works flawlessly:
http://www.freedownloadscenter.com/Programming/Components_and_Libraries/BytesRoad_NetSuit_Library.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.IO;
using System.Configuration;
using System.Collections.Specialized;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using BytesRoad.Net.Ftp;
namespace GetMedia
{
class Program
{
static void Main(string[] args)
{
string strPath;
string strThumbPath;
string strThumbLocalPath;
string strURLRoot;
string strVideoFile;
string strThumbfile;
string strError;
BizetDataDataContext db = new BizetDataDataContext();
VCMediaDataContext db2 = new VCMediaDataContext();
db.Connection.ConnectionString = Settings.Default.ConnectionString;
db2.Connection.ConnectionString = Settings.Default.ConnectionString;
//Temp Folder
strPath = Settings.Default.TempFolder;
strThumbLocalPath = Settings.Default.ThumbPath;
download video and thumb
//then upload to mediaserver
IQueryable<BizetInfo> custQuery =
from bizet in db.BizetInfos
where bizet.Path != null
select bizet;
foreach (BizetInfo objbizet in custQuery)
{
//Grab filename and path
strVideoFile = Path.GetFileName(objbizet.Path).Replace("%20", "_").Replace("_medium", "").Replace(" ", "_");
strThumbfile = Path.GetFileName(objbizet.Path).Replace("%20", " ").Replace("_medium.wmv", ".mpg.png");
strURLRoot = objbizet.Path.Replace(Path.GetFileName(objbizet.Path), "");
strThumbPath = strURLRoot + strThumbfile;
strError = "";
try
{
wsViaCastMedia.MediaTransferSoapClient ws = new wsViaCastMedia.MediaTransferSoapClient();
System.Net.WebClient wc = new System.Net.WebClient();
//connect to Bizet
Console.WriteLine("Starting spotID: " + objbizet.SPOTID.ToString().Trim());
Console.WriteLine("connected to ws");
Console.WriteLine("Downloading Video File");
//Download Video
wc.DownloadFile(objbizet.Path, strPath + strVideoFile);
//Download Thumb
Console.WriteLine("Downloading Thumb File");
wc.DownloadFile(strThumbPath, strThumbLocalPath + strThumbfile);
wc.Dispose();
//new ftp code
BytesRoad.Net.Ftp.FtpClient f = new BytesRoad.Net.Ftp.FtpClient();
f.PassiveMode = false;
f.Connect(999999999, "IPADDRESS OF FTP", 21);
f.Login(999999999, "", "");
try
{
f.ChangeDirectory(999999999, objbizet.CLIENTID.ToString().Trim());
}
catch (Exception e)
{
f.CreateDirectory(999999999, objbizet.CLIENTID.ToString().Trim());
f.ChangeDirectory(999999999, objbizet.CLIENTID.ToString().Trim());
Console.WriteLine(e);
}
f.PutFile(999999999, strVideoFile, "E:\\temp\\" + strVideoFile);
Console.WriteLine("Transfer of Video File " + objbizet.Path + " Complete");
//response.Close();
f.Disconnect(999999999);
}
catch (Exception e)
{
Console.WriteLine(e);
strError = e.ToString();
}
finally //Update Data
{
//check if spot Exists ///need to fix
//var myquery = from m in db2.Medias
// where m.SpotID == Convert.ToInt32(objbizet.SPOTID.Trim())
// select m;
//foreach (var mm in myquery)
//{
// //db2.DeleteMedia(objbizet.SPOTID.Trim());
//}
if (strError == "")
{
db2.AddMedia(Convert.ToInt32(objbizet.SPOTID), objbizet.Title, objbizet.Keywords, objbizet.Path, strVideoFile, objbizet.CLIENTNAME, Convert.ToInt32(objbizet.CLIENTID), objbizet.SUBCATEGORYNAME, Convert.ToInt32(objbizet.SUBCATEGORYID), Convert.ToDecimal(objbizet.PRICE), strThumbfile, objbizet.Description);
}
else
{
db2.AddMedia(Convert.ToInt32(objbizet.SPOTID), "Under Maintenance - " + objbizet.Title, objbizet.Keywords, objbizet.Path, strVideoFile, objbizet.CLIENTNAME, Convert.ToInt32(objbizet.CLIENTID), objbizet.SUBCATEGORYNAME, Convert.ToInt32(objbizet.SUBCATEGORYID), Convert.ToDecimal(objbizet.PRICE), strThumbfile, objbizet.Description);
}
}
}
//dispose
db.Dispose();
db2.Dispose();
}
}
}

Resources