Serialize a FileStream for transport - wcf

See also this question: Can I pass a System.Net.MailMessage to a WCF service?
I'd like to add attachments to the mail being sent. Attachments are either files on the local disc, or dynamically created Streams. A WCF contract can contain a Stream, but only when all arguments are of type Stream. So, what is the best way to pass one or more Attachments to a WCF service?

Alright I've solved this myself. The trick here is to convert the attachment to a Base64 encodes string, much the same way email systems do this. I've created a class to handle this. Posted here for others:
[DataContract]
public class EncodedAttachment
{
[DataMember(IsRequired=true)]
public string Base64Attachment;
[DataMember(IsRequired = true)]
public string Name;
/// <summary>
/// One of the System.Net.Mime.MediaTypeNames
/// </summary>
[DataMember(IsRequired = true)]
public string MediaType;
}
public EncodedAttachment CreateAttachment(string fileName)
{
EncodedAttachment att = new EncodedAttachment();
if (!File.Exists(fileName))
throw new FileNotFoundException("Cannot create attachment because the file was not found", fileName);
FileInfo fi = new FileInfo(fileName);
att.Name = fi.Name;
att.MediaType = System.Net.Mime.MediaTypeNames.Text.Plain;
using (FileStream reader = new FileStream(fileName, FileMode.Open))
{
byte[] buffer = new byte[reader.Length];
reader.Read(buffer, 0, (int)reader.Length);
att.Base64Attachment = Convert.ToBase64String(buffer);
}
return att;
}
And on the client side:
public void SendEmail(SmallMessage msg)
{
using (MailMessage message = new MailMessage())
{
message.Body = msg.Body;
message.Subject = msg.Subject;
message.To.Add(new MailAddress(msg.To));
message.From = new MailAddress(msg.From);
foreach (EncodedAttachment att in msg.Attachments)
{
message.Attachments.Add(CreateAttachment(att));
}
SmtpClient client = new SmtpClient();
client.Send(message);
}
}
Attachment CreateAttachment(EncodedAttachment encodedAtt)
{
MemoryStream reader = new MemoryStream(Convert.FromBase64String(encodedAtt.Base64Attachment));
Attachment att = new Attachment(reader, encodedAtt.Name, encodedAtt.MediaType);
return att;
}

Related

The data changes when I try to pull it back C#

I'm trying to save a generic list and get it back by using a BinaryFormatter but I can't get the list in the form that I have saved, it returns me only the first item in the list. I think there might be an error while the code tries not to overwrite the file. If you need more details, please tell me and I'll add the details that you need.
#region Save
/// <summary>
/// Saves the given object to the given path as a data in a generic list.
/// </summary>
protected static void Save<T>(string path, object objectToSave)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream;
if (!File.Exists(path))
{
stream = File.Create(path);
}
else
{
stream = File.Open(path, FileMode.Open);
}
List<T> list = new List<T>();
try
{
list = (List<T>)formatter.Deserialize(stream);
}
catch
{
}
list.Add((T)objectToSave);
formatter.Serialize(stream, list);
stream.Close();
}
#endregion
#region Load
/// <summary>
/// Loads the data from given path and returns a list of questions.
/// </summary>
protected static List<T> Load<T>(string path)
{
if (!File.Exists(path))
{
System.Windows.Forms.MessageBox.Show(path + " yolunda bir dosya bulunamadı!");
return null;
}
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = File.Open(path, FileMode.Open);
List<T> newList;
try
{
newList = (List<T>)formatter.Deserialize(stream);
}
catch
{
newList = null;
}
stream.Close();
return newList;
}
#endregion
Okey, I just figured the problem. Appearently if you make a change in the data without saving it (I did it in "list = (List)formatter.Deserialize(stream);" this line of code) and then if you try to serialize it again, the FileStrem that you are using doesn't work generically, so you have to close the old stream and than reopen it or another again or just simply type stream = File.Open(path, FileMode.Open); again. Thanks anyway :D

How to pass client certificate using AutoRest client

We are using AutoRest for generating client code based on API Swagger files.
I'm trying to pass client certificate to the API. But noticed that generated client code doesn't accept WebRequestHandler.
Generated code looks like below:
public MyTestApiV1(Uri baseUri, params DelegatingHandler[] handlers) : this(handlers)
{
if (baseUri == null)
{
throw new ArgumentNullException("baseUri");
}
this.BaseUri = baseUri;
}
I feel like I'm missing something here. Has anyone managed to send client certificate using AutoRest?
Tried this but webRequestHandler is always null:
var webRequestHandler = client.HttpMessageHandlers.First() as WebRequestHandler;
if (webRequestHandler != null)
{
var secretRetrieved = keyVault.GetSecretAsync("my-cert");
var pfxBytes = Convert.FromBase64String(secretRetrieved.Result);
// or recreate the certificate directly
var certificate = new X509Certificate2(pfxBytes);
webRequestHandler.ClientCertificates.Add(certificate);
}
You can use another overloaded constructor:
/// <summary>
/// Initializes ServiceClient using base HttpClientHandler and list of handlers.
/// </summary>
/// <param name="rootHandler">Base HttpClientHandler.</param>
/// <param name="handlers">List of handlers from top to bottom (outer handler is the first in the list)</param>
protected ServiceClient(HttpClientHandler rootHandler, params DelegatingHandler[] handlers)
ServiceClient is the base class for generated clients. Therefore, code might look like:
var secretRetrieved = keyVault.GetSecretAsync("my-cert");
var pfxBytes = Convert.FromBase64String(secretRetrieved.Result);
// or recreate the certificate directly
var certificate = new X509Certificate2(pfxBytes);
WebRequestHandler webRequestHandler = new WebRequestHandler();
webRequestHandler.ClientCertificates.Add(certificate);
var client = new MyTestApiV1(webRequestHandler);
client.BaseUri = baseUri;
.net Core version
Ivan R's answer led me on the right path but it's a little different for .net core (2.2 at this point in time) as WebRequestHandler is not available in core.
I had to use a pfx file and password in my case. GetNumberPassedIn isn't in the generic Petstore Swagger template but was what I was testing with.
Program.cs:
using System;
using System.Net.Http;
namespace SimpleApi2.Console
{
class Program
{
static void Main(string[] args)
{
var certificate = new CertInfo().GetCertFromPfx(Const.PfxPath, Const.PfxPassword);
var handler = new HttpClientHandler();
handler.ClientCertificates.Add(certificate);
var client = new HttpClient(handler);
var petStore = new SwaggerPetstore(client, true);
petStore.BaseUri = new Uri(Const.PublicUrl);
var result = petStore.GetNumberPassedIn(135, Const.ApiKey);
System.Console.WriteLine(result.ToString());
System.Console.ReadKey();
}
}
}
CertInfo.cs:
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Security;
namespace SimpleApi2.Console
{
class CertInfo
{
internal static byte[] ReadFile(string fileName)
{
FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read);
int size = (int)f.Length;
byte[] data = new byte[size];
f.Read(data, 0, size);
f.Close();
return data;
}
public CertInfo() { }
public X509Certificate2 GetCertFromPfx(string pfxFilePath, string password)
{
try
{
byte[] rawData = ReadFile(pfxFilePath);
var passwordAsChars = password.ToCharArray();
var securePassword = new SecureString();
foreach (char c in password)
securePassword.AppendChar(c);
securePassword.MakeReadOnly();
X509Certificate2 x509 = new X509Certificate2(pfxFilePath, password,
X509KeyStorageFlags.UserKeySet);
WriteCertInfo(x509);
return x509;
}
catch (DirectoryNotFoundException)
{
System.Console.WriteLine("Error: The directory specified could not be found.");
throw;
}
catch (IOException)
{
System.Console.WriteLine("Error: A file in the directory could not be accessed.");
throw;
}
catch (NullReferenceException)
{
System.Console.WriteLine("File must be a .cer file. Program does not have access to that type of file.");
throw;
}
}
private static void WriteCertInfo(X509Certificate2 x509)
{
//Print to console information contained in the certificate.
System.Console.WriteLine("{0}Subject: {1}{0}", Environment.NewLine, x509.Subject);
System.Console.WriteLine("{0}Issuer: {1}{0}", Environment.NewLine, x509.Issuer);
System.Console.WriteLine("{0}Version: {1}{0}", Environment.NewLine, x509.Version);
System.Console.WriteLine("{0}Valid Date: {1}{0}", Environment.NewLine, x509.NotBefore);
System.Console.WriteLine("{0}Expiry Date: {1}{0}", Environment.NewLine, x509.NotAfter);
System.Console.WriteLine("{0}Thumbprint: {1}{0}", Environment.NewLine, x509.Thumbprint);
System.Console.WriteLine("{0}Serial Number: {1}{0}", Environment.NewLine, x509.SerialNumber);
System.Console.WriteLine("{0}Friendly Name: {1}{0}", Environment.NewLine, x509.PublicKey.Oid.FriendlyName);
System.Console.WriteLine("{0}Public Key Format: {1}{0}", Environment.NewLine, x509.PublicKey.EncodedKeyValue.Format(true));
System.Console.WriteLine("{0}Raw Data Length: {1}{0}", Environment.NewLine, x509.RawData.Length);
System.Console.WriteLine("{0}Certificate to string: {1}{0}", Environment.NewLine, x509.ToString(true));
}
}
}

passing object and stream to rest wcf

Here, when I am trying to pass both object and stream to wcf operation. I am getting "bad request 400" exception. If I pass only stream it is working fine with no issues and I am able to get output as stream. Any suggestions are greatly appreciated.
Client side code:
testclass tcls = new testclass();
tcls.name = "myclass";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(string.Format(#"http://localhost:225141/RestService.svc/getobject/tc/{0}",tcls));
string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("bda11d91-7ade-4da1-855d-24adfe39d154"));
req.Headers.Add("Authorization", "Basic " + svcCredentials);
req.MaximumResponseHeadersLength = 2147483647;
req.Method = "POST";
req.ContentType = "application/octet-stream";
FileStream fs = new FileStream("file1.txt", FileMode.Open, FileAccess.Read);
MemoryStream ms = new MemoryStream();
fs.CopyTo(ms);
ms.Position = 0;
byte[] dd = ms.ToArray();
Stream strw = req.GetRequestStream();
strw.Write(dd.ToArray(), 0, dd.Length);
strw.Close();
// here i am getting "bad request error"
using (WebResponse svcResponse = (HttpWebResponse)req.GetResponse())
{
MemoryStream msm = new MemoryStream();
svcResponse.GetResponseStream().CopyTo(msm);
msm.Position = 0;
byte[] data = msm.ToArray();
}
...
Service side code:
//IRestService.cs
[ServiceContract]
public interface IRestService
{
[OperationContract]
[WebInvoke(UriTemplate="getobject/tc/{tc}",Method="POST",
BodyStyle=WebMessageBodyStyle.Wrapped,ResponseFormat=WebMessageFormat.Json)]
Stream getobjectl(testclass obj,Stream tc);
}
[DataContract]
[KnownType(typeof(testclass))]
public class testclass
{
[DataMember]
public string name { get; set; }
}
//RestService.cs
public Stream getobject(testclass tc, Stream st)
{
//code;
}

Calling Webmethod containing byte array (as in paramaeter)parameter with ksoap2 on android

There is WEB service written on C# with next method:
[WebMethod]
public string ByteArrTest(byte[] Buffer)
{
if (Buffer == null) return "buffer is null";
else return Buffer.Length.ToString() + " is buffer length";
}
i 'ld like call this method from android device using Ksoap2 library alike belove (simplified):
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
new MarshalBase64().register(envelope);
envelope.encodingStyle = SoapEnvelope.ENC;
SoapObject request = new SoapObject(this.getNameSpace(), this.getMethodName());
PropertyInfo pi4 = new PropertyInfo();
pi4.setName("Buffer");
byte [] b="this text".getBytes();
pi4.setValue(b);
pi4.setType(byte[].class);
// request.addProperty("buffer", "bytes".getBytes);
request.addProperty(pi4);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport =
new HttpTransportSE(this.getURL());//
androidHttpTransport.call(this.getSoapAction(), envelope);
Object response = envelope.getResponse();
//next implementation
Responce always is "buffer is null"
what is incorrect or wrong?
Thanks for any attention
Posting the whole of your method in Android calling the web service would help more.
I'm using KSoap in an Android project I'm currently working on and I'm retrieving strings. Heres one of my methods modified to match what you need:
private static String NAMESPACE = "http://tempuri.org/";
private static String SOAP_ACTION = "http://tempuri.org/";
private static final String URL = "Your url link to your web services asmx file";
public static String ByteArrTestCall(byte[] t) {
String resTxt = null;
SoapObject request = new SoapObject(NAMESPACE, "ByteArrTest");
// Add the property to request object
request.addProperty("Buffer", t);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try
{
androidHttpTransport.call(SOAP_ACTION+"ByteArrTest", envelope);
SoapPrimitive receivedString = (SoapPrimitive) envelope.getResponse();
resTxt = receivedString.toString();
}
catch(Exception e)
{
resTxt = androidHttpTransport.requestDump;
return e.toString() + resTxt;
}
return resTxt;
}

Programmatically upload XSN to SharePoint

I have a bunch of InfoPath form templates (xsn) which I want to upload to a SharePoint list programmatically. My program has to upload these form templates to different lists based on predefined logic. When I upload the browser-enabled form templates (xsn) with my code, the forms do not work:
/// <summary>
/// Uploads a file to the specified sharepoint list
/// </summary>
/// <param name="listName"></param>
/// <param name="fileInfo"></param>
/// <param name="listVersion"></param>
/// <returns></returns>
public static bool UploadFile(string listName, FileInfo fileInfo, string listVersion)
{
WebRequest request = WebRequest.Create(fileInfo.URL);
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
request.Method = "PUT";
byte[] buffer = new byte[1024];
using (Stream stream = request.GetRequestStream())
{
using (MemoryStream ms = new MemoryStream(fileInfo.Bytes))
{
for (int i = ms.Read(buffer, 0, buffer.Length); i > 0; i = ms.Read(buffer, 0, buffer.Length))
stream.Write(buffer, 0, i);
}
}
WebResponse response = request.GetResponse();
response.Close();
var client = new Lists.ListsSoapClient();
var batch = new XElement("Batch",
new XAttribute("OnError", "Continue"),
new XAttribute("ListVersion", listVersion),
new XAttribute("PreCalc", "TRUE"));
var method = new XElement("Method",
new XAttribute("ID", "1"),
new XAttribute("Cmd", "Update"),
new XElement("Field",
new XAttribute("Name", "ID")),
new XElement("Field",
new XAttribute("Name", "FileRef"),
fileInfo.URL));
foreach (string key in fileInfo.Properties.Keys)
{
object value = fileInfo.Properties[key];
method.Add(new XElement("Field",
new XAttribute("Name", key),
fileInfo.Properties[key]));
}
batch.Add(method);
var element = client.UpdateListItems(listName, batch);
var code = element.Elements().First().Elements().First().Value;
if (code != "0x00000000")
throw new Exception(code);
return true;
}
It seems there is more to be done that just pushing a file stream into the list.
Anyone have an idea how to do this?
EDIT More specifically, the error message I get is: This form template is not enabled for viewing in the browser.
UPDATE When I publish the same form with Microsoft InfoPath it works.
you can use this code to convert your uploaded form in browser enabled form as:
FormsService localFormsService;
SPFarm localFarm = SPFarm.Local;
SPSite localSite = new SPSite("http://ServerName");
SPWeb localWeb = localSite.AllWebs["SiteName"];
try
{
localFormsService = localFarm.Services.GetValue<FormsService>(FormsService.ServiceName);
SPFile localFile = localWeb.GetFile("FormLibrary/Forms/FormTemplate.xsn");
localFormsService.BrowserEnableUserFormTemplate(localFile);
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.Write("Press Enter to Continue");
Console.ReadLine();
}
or you can use this link for more details as:
http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.server.administration.formsservice.browserenableuserformtemplate.aspx