This error usually occurs when you have set up Visual Studio to debug an existing web application running in IIS rather than the built in ASP.NET debug web server.
IIS by default listens for web requests on port 80. In this case, another application is already listening for requests on port 80. Typically, the offending application is Skype or Apache which by default takes over listening on ports 80 and 443 when installed.
If you do development in Apache too go to httpd.conf and find for below lines:
Here’s a simplified snippet for sending an in-memory string as an email attachment (a CSV file in this particular case).
If you are trying this with PDF, put special mention on stream.position = 0. As it will make the writer write the file from starting position always.
Whatever the source-type you care coming with convert that to memory-stream and the use the memory stream to create the attachment.
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream)) // using UTF-8 encoding by default
using (var mailClient = new SmtpClient("localhost", 25))
using (var message = new MailMessage("firstname.lastname@example.org", "email@example.com", "Just testing", "See attachment..."))
stream.Position = 0; // read from the start of what was written
message.Attachments.Add(new Attachment(stream, "filename.csv", "text/csv"));
And if you want to send JSON into your action method and not a model, simply bind to a string instead of a model. In action method you can deserialize this string to a json model or key-value pair and then can do whatever you want with the JSON string inside your action method.
If you work on Web Applications and using Asp.Net to power it you may find this error on run time, application might compile perfectly but generates the runtime error stating:
Could not load file or assembly ‘Microsoft.Web.Infrastructure, Version=184.108.40.206, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified.
It appears that Microsoft.Web.Infrastructure.dll is not being installed in the GAC, even if .net (4.0 or 4.5 or other) are installed successfully on Windows Server. On localhost (typically Windows client), it seems like it is being in the GAC when the tools/platform (Visual Studio etc.) are installed.
Possible Fix 1
Run the following command in the Package Manager Console. (If you are using Visual Studio, this can be reached via menu options “Tools –> Library Package Manager –> Package Manager Console:)
PM> Install-Package Microsoft.Web.Infrastructure
You will see the following messages if it is successfully installed.
Successfully installed ‘Microsoft.Web.Infrastructure 220.127.116.11’.
Successfully added ‘Microsoft.Web.Infrastructure 18.104.22.168’ to Web.
If you see this message you are good to go. Shoop. But in case it does not work try the next one.
Possible Fix 2 (Less Technical)
When you do above fix package manager can produce this(non welcoming) message for you:
the package was already installed and assigned to my project.
This is the case when you already have this Microsoft.Web.Infrastructure.dll added in your solution. Go to Window’s explorer and try to search the file by name. Most probably you will find it, copy the path and add it as a reference in project.
Rebuild and Run the project.
Let me know in comments if this does not work or some other solution works for you.