IsolatedStorageException in Ms Word Addin - vb.net

I created a Word add-in project with a ribbon, and when I want to save the document, after several modifications with OpenXml, an exception is raised.
Dim MainXMLDoc As New XmlDocument()
Using WordDoc As WordprocessingDocument = WordprocessingDocument.Open(DocPath, True)
Dim mainPart As MainDocumentPart = WordDoc.MainDocumentPart
If Not mainPart Is Nothing Then
MainXMLDoc.Load(mainPart.GetStream())
EXmlDocument.XMLDoc = Nothing
EXmlDocument.XMLDoc = MainXMLDoc
EXmlDocument.GetWordDocIds()
'..............
end if
'........
Dim stream As IO.Stream
stream = mainPart.GetStream(FileMode.Create, FileAccess.Write)
MainXMLDoc.Save(stream) '-----> exception
And the exception message is:
Interception de System.IO.IsolatedStorage.IsolatedStorageException
Message=Unable to determine the identity of domain. Source=mscorlib
StackTrace:
at System.IO.IsolatedStorage.IsolatedStorage._GetAccountingInfo(Evidence
evidence, Type evidenceType, IsolatedStorageScope fAssmDomApp, Object&
oNormalized)
at System.IO.IsolatedStorage.IsolatedStorage.GetAccountingInfo(Evidence
evidence, Type evidenceType, IsolatedStorageScope fAssmDomApp, String&
typeName, String& instanceName)
at System.IO.IsolatedStorage.IsolatedStorage._InitStore(IsolatedStorageScope
scope, Evidence domainEv, Type domainEvidenceType, Evidence assemEv,
Type assemblyEvidenceType, Evidence appEv, Type appEvidenceType)
at System.IO.IsolatedStorage.IsolatedStorage.InitStore(IsolatedStorageScope
scope, Type domainEvidenceType, Type assemblyEvidenceType)
at System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(IsolatedStorageScope
scope, Type domainEvidenceType, Type assemblyEvidenceType)
at MS.Internal.IO.Packaging.PackagingUtilities.ReliableIsolatedStorageFileFolder.GetCurrentStore()
at MS.Internal.IO.Packaging.PackagingUtilities.ReliableIsolatedStorageFileFolder..ctor()
at MS.Internal.IO.Packaging.PackagingUtilities.GetDefaultIsolatedStorageFile()
at MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName(Int32
retryCount, String& fileName)
at MS.Internal.IO.Packaging.SparseMemoryStream.SwitchModeIfNecessary()
at MS.Internal.IO.Packaging.SparseMemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at MS.Internal.IO.Packaging.CompressEmulationStream.Write(Byte[] buffer,
Int32 offset, Int32 count)
at MS.Internal.IO.Packaging.CompressStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Write(Byte[]
buffer, Int32 offset, Int32 count)
at MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Write(Char value)
at System.Xml.XmlTextWriter.Indent(Boolean beforeEndElement)
at System.Xml.XmlTextWriter.AutoComplete(Token token)
at System.Xml.XmlTextWriter.WriteStartElement(String prefix, String localName, String ns)
at System.Xml.XmlDOMTextWriter.WriteStartElement(String prefix, String localName, String ns)
at System.Xml.XmlElement.WriteStartElement(XmlWriter w)
at System.Xml.XmlElement.WriteElementTo(XmlWriter writer, XmlElement e)
at System.Xml.XmlElement.WriteTo(XmlWriter w)
at System.Xml.XmlDocument.WriteContentTo(XmlWriter xw)
at System.Xml.XmlDocument.WriteTo(XmlWriter w)
at System.Xml.XmlDocument.Save(Stream outStream) InnerException:
This problem appears when the document size is larger than 1 MB.
After several searches, the 'save' action is made with an isolated storage, and the solutions is :
Install with Clickonce
Create new domain
Modify registry.
But for this project, I can't use ClickOnce and I can't modify the registry.
So I made changes to my source code, to create a new domain.
Imports DocumentFormat.OpenXml.Packaging
Imports System.IO
<Serializable()> Public Class ToIsolatedPackageSave
Public Sub Save(ByRef mainPart As MainDocumentPart, ByRef xmlDocument As Xml.XmlDocument)
Dim stream As IO.Stream
stream = mainPart.GetStream(FileMode.Create, FileAccess.Write)
xmlDocument.Save(stream) -----> same exception
End Sub
End Class
And
Dim stream As Stream
Dim isolatedPackageSave As ToIsolatedPackageSave
Dim isolatedAppDomain As AppDomain
Try
Dim isolatedAppDomainSetup As AppDomainSetup = New AppDomainSetup()
isolatedAppDomainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory
Dim isolatedEvidence As Evidence = New Evidence(AppDomain.CurrentDomain.Evidence)
isolatedEvidence.AddAssembly(Reflection.Assembly.GetExecutingAssembly().FullName)
isolatedEvidence.AddHost(New Zone(Security.SecurityZone.MyComputer))
isolatedAppDomain = AppDomain.CreateDomain("TrustIsolatedDomain", isolatedEvidence, isolatedAppDomainSetup)
isolatedPackageSave = isolatedAppDomain.CreateInstanceAndUnwrap(GetType(ToIsolatedPackageSave).Assembly.FullName, GetType(ToIsolatedPackageSave).FullName)
'(IsolatedPackageSave)isolatedAppDomainSetup.CreateInstanceAndUnwrap(GetType(ToIsolatedPackageSave).Assembly.FullName, GetType(ToIsolatedPackageSave).FullName)
isolatedPackageSave.Save(mainPart, MainXMLDoc)
Catch ex As Exception
Finally
AppDomain.Unload(isolatedAppDomain)
End Try
But this code doesn't fix my problem.

I followed Tim Lewis' advice in this post to inject the needed security evidence:
http://rekiwi.blogspot.com/2008/12/unable-to-determine-identity-of-domain.html
public void VerifySecurityEvidenceForIsolatedStorage(Assembly assembly)
{
var isEvidenceFound = true;
var initialAppDomainEvidence = System.Threading.Thread.GetDomain().Evidence;
try
{
// this will fail when the current AppDomain Evidence is instantiated via COM or in PowerShell
using (var usfdAttempt1 = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForDomain())
{
}
}
catch (System.IO.IsolatedStorage.IsolatedStorageException e)
{
isEvidenceFound = false;
}
if (!isEvidenceFound)
{
initialAppDomainEvidence.AddHostEvidence(new Url(assembly.Location));
initialAppDomainEvidence.AddHostEvidence(new Zone(SecurityZone.MyComputer));
var currentAppDomain = Thread.GetDomain();
var securityIdentityField = currentAppDomain.GetType().GetField("_SecurityIdentity", BindingFlags.Instance | BindingFlags.NonPublic);
securityIdentityField.SetValue(currentAppDomain, initialAppDomainEvidence);
var latestAppDomainEvidence = System.Threading.Thread.GetDomain().Evidence; // setting a breakpoint here will let you inspect the current app domain evidence
}
}
Then call the following code at startup:
VerifySecurityEvidenceForIsolatedStorage(this.GetType().Assembly);

I think, you can't use OpenXML inside office addins. If you want to use OpenXML, u have to do it offline (when document is not opened). You can only use COM interops (office object Model calls) from inside addins.

Related

Unable to parse tnef part from MimeMessage

I have a MimeMessage which contains a winmail.dat attachment of type application/ms-tnef.
I believe this is an RTF formatted email, which is sent from Outlook client. I tried parsing it with the tnefpart but I'm running into the following error.
Here is my function:
ProcessMimeMessage(MimeMessage plainMime)
{
foreach (var attachment in plainMime.Attachments)
{
if (attachment.GetType() == typeof(MimeKit.Tnef.TnefPart))
{
ReportStatus("Processing Tnef Part (RTF Message): ");
MimeKit.Tnef.TnefPart tnefPart = (MimeKit.Tnef.TnefPart)attachment;
try {
MimeMessage mimeMessage = tnetPart.ConvertToMessage();
} catch (Exception ex) {printexception(ex);}
}
}
}
This is the exception that is thrown from ConvertToMessage
Invalid addr-spec token at offset 0
at MimeKit.InternetAddress.TryParseAddrspec(Byte[] text, Int32& index, Int32 endIndex, Byte[] sentinels, Boolean throwOnError, String& addrspec, Int32& at)
at MimeKit.MailboxAddress.set_Address(String value)
at MimeKit.Tnef.TnefPart.EmailAddress.TryGetMailboxAddress(MailboxAddress& mailbox)
at MimeKit.Tnef.TnefPart.ExtractMapiProperties(TnefReader reader, MimeMessage message, BodyBuilder builder)
at MimeKit.Tnef.TnefPart.ExtractTnefMessage(TnefReader reader)
at MimeKit.Tnef.TnefPart.ConvertToMessage()
Thanks
This just means that the "email address" in one of the TNEF fields is not a valid email address (syntactically).
It might just be that the address type field was not "SMTP" and MimeKit tried to parse it as if it were a rfc822 address field.
I've improved the robustness in the following commit: https://github.com/jstedfast/MimeKit/commit/15f955b49dc7743d1281afbedce6d327706e161b
You can use the MyGet packages listed in the README.md file to get the fix for this.

SSIS (File System Task) - Unable to move file [duplicate]

I've created a connection using Microsoft Office 12.0 Access Database Engine OLE DB Provider as Excel Schema to loop through all the sheets in the Excel file as demonstrated in this question How to loop through Excel files and load them into a database using SSIS package?
And using Foreach ADO.NET Schema Rowset Enumerator to loop through the excel files.
Everything is working fine now, but after importing the data from Excel, I wanted to move that file to Archive folder. And tried using a File System Task, but I get the error as
[File System Task] Error: An error occurred with the following error message: "The process cannot access the file because it is being used by another process.".
And I also tried with script task from this link. But I was getting some error and couldn't solve the error as I've got zero knowledge on C#.
Below is the error I've got when I tried to move the files using a script task.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript().
Update:
Here's my complete code with which I'm trying to move the files.
If I add a breakpoint at enum ScriptResults, I don't get that popup and the task gets completed successfully and the file is also been moved to archive, but if I don't add any breakpoint in the C# code, I get that pop and the file is not moved to archive.
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
#endregion
namespace ST_9fc6ad7db45c4a7bb49f303680f789ef
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
DirectoryInfo di = new DirectoryInfo(Dts.Variables["SplitSkill_FolderPath"].Value.ToString());
FileInfo[] fi = di.GetFiles("*.xlsx");
String filename = fi[0].Name;
string sourceFileName = filename;
string destinationFile = #"D:\Flipkart\Data\Split Skill\Archive\" + sourceFileName;
string sourceFile = #"D:\Flipkart\Data\Split Skill\" + sourceFileName;
if (File.Exists(destinationFile))
File.Delete(destinationFile);
// To move a file or folder to a new location:
System.IO.File.Move(sourceFile, destinationFile);
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
As far as I understand, I think you have a task for looping through sheets and another for looping through files. So you probably have 2 tasks inside a foreach loop. Try making a copy of the file inside the foreach loop with a system task
for the executable
c:\windows\system32\cmd.exe
and for the arguments soemthing like
C COPY "C:\xxx\abc\\Destination_template.accdb" "C:\xxx\abc\\Destination_template.accdb"Destination_template - Kopie.accdb"
Then create a file system task which moves that copy to your archive.
This should do the trick (Maybe not the best approach but should work)

NReco.PdfGenerator The pipe has been ended error on the server

I'm using NReco.PdfGenerator for my PDF documents(that component is based on WkHtmlToPdf tool), my code allow me to create a pdf calling a function with the parameters controller, action, model:
public static byte[] GeneratePdfDocument(System.Web.Mvc.Controller controller, string viewName, object model)
{
string result;
controller.ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
result = sw.ToString();
}
return (new NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf(result);
}
With this code I can create a PDF easily from my views and it's working great on my development environment but on a server I'm getting this error:
The pipe has been ended.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.IOException: The pipe has been ended.
[IOException: The pipe has been ended.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +11185413
System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count) +10770013
System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count) +139
NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(String htmlFilePath, Byte[] inputBytes, String coverHtml, String outputPdfFilePath, Stream outputStream) +2166
[Exception: Cannot generate PDF: The pipe has been ended.]
NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(String htmlFilePath, Byte[] inputBytes, String coverHtml, String outputPdfFilePath, Stream outputStream) +2734
NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent, String coverHtml, Stream output) +51
NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent, String coverHtml) +42
I think that is maybe a problem with permissions or some configuration on my IIS, any idea?
You are probably missing Visual C++ Redistributable Packages for VS2013 (x86). Even if your server is running 64bit Windows, you need to install the x86 C++ Packages.

#HhtmlDropdown throw SerializationException Exception

I have a form that has a DropDownList. I am using SQLServer State mode to save the session in SQL Server. The form is displayed correctly; however, it throws an exception during the form submission. I've been looking and tried some of the suggested answer that were posted online with no luck. What am I doing wrong here?
//This is the section where I am building the SelectList Item in the controller
var ListDepartment = ManageUsersMembership.GetDepartment(SessionWrapper.studentBookTrade.SelectedSchoolID);
// book.DepartmentList = ListDepartment; //new SelectList(ListDepartment, "DepartmentID", "DepartmentName", SessionWrapper.studentBookTrade.SelectedDepartmentID);
List<SelectListItem> lDepartment = new List<SelectListItem>();
foreach (var item in ListDepartment)
{
if (SessionWrapper.studentBookTrade.SelectedDepartmentID > 0 && item.DepartmentID == SessionWrapper.studentBookTrade.SelectedDepartmentID)
{
lDepartment.Add(new SelectListItem { Text = item.DepartmentName, Value = item.DepartmentID.ToString(), Selected = true });
}
else
{
lDepartment.Add(new SelectListItem { Text = item.DepartmentName, Value = item.DepartmentID.ToString() });
}
}
book.DepartmentList = lDepartment;
//this is my property in the Model:
public virtual IEnumerable<SelectListItem> DepartmentList { get; set; }
//This is the HTML Code in my View:
#Html.DropDownListFor(model => model.DepartmentID, new SelectList(Model.DepartmentList, "Value", "Text"), "Select Department", new {#class="form-control", id="DepartmentID", onchange = "ValueSelected()" })
#Html.ValidationMessageFor(model => model.DepartmentID)
//The exception
Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[SerializationException: Type 'System.Web.Mvc.SelectList' in Assembly 'System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.]
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) +10751795
System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +230
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +121
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SerializationBinder binder) +185
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) +565
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +446
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +131
System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1666
[HttpException (0x80004005): Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.]
System.Web.Util.AltSerialization.WriteValueToStream(Object value, BinaryWriter writer) +1754
System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(Object value, BinaryWriter writer) +34
System.Web.SessionState.SessionStateItemCollection.Serialize(BinaryWriter writer) +628
System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData item, Stream stream) +240
System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData item, Int32 initialStreamSize, Byte[]& buf, Int32& length, Boolean compressionEnabled) +62
System.Web.SessionState.SqlSessionStateStore.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem) +135
System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) +565
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
I think you have too much here. Try changing your code this way
public IEnumerable<SelectListItem> DepartmentList { get; set; }
I usually use List but ienumerable should work too
then on your view you don't need to cast the list
#Html.DropDownListFor(model => model.DepartmentID, Model.DepartmentList, "Select Department", new {#class="form-control", id="DepartmentID", onchange = "ValueSelected()" })
the "for" helper will map the list for you. See if this helps

WCF + REST: Where is the request data?

I'm currently developing a WCF RESTful service. Within the validation of the POST data, I am throwing exceptions if the request XML does not conform to our business rules.
The goal is to send an e-mail to the appropriate staff if a request comes in that considered invalid. But, along with the incoming request headers, method and URI, I'd like to also send the XML that was posted.
I have not been able to find a way to access this data. Is WCF actually destroying the request body/data before I have a chance to access it or am I missing something?
Your help is appreciated as I'm confused as to why I can't access the request data.
This unfortunately isn't supported- we had a similar need, and did it by calling internal members with reflection. We just use it in an error handler (so we can dump the raw request), but it works OK. I wouldn't recommend it for a system you don't own and operate though (eg, don't ship this code to a customer), since it can change at any time with a service pack or whatever.
public static string GetRequestBody()
{
OperationContext oc = OperationContext.Current;
if (oc == null)
throw new Exception("No ambient OperationContext.");
MessageEncoder encoder = oc.IncomingMessageProperties.Encoder;
string contentType = encoder.ContentType;
Match match = re.Match(contentType);
if (!match.Success)
throw new Exception("Failed to extract character set from request content type: " + contentType);
string characterSet = match.Groups[1].Value;
object bufferedMessage = operationContextType.InvokeMember("request",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField,
null, oc, null);
//TypeUtility.AssertType(bufferedMessageType, bufferedMessage);
object messageData = bufferedMessageType.InvokeMember("MessageData",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty,
null, bufferedMessage, null);
//TypeUtility.AssertType(jsonBufferedMessageDataType, messageData);
object buffer = jsonBufferedMessageDataType.InvokeMember("Buffer",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty,
null, messageData, null);
ArraySegment<byte> arrayBuffer = (ArraySegment<byte>)buffer;
Encoding encoding = Encoding.GetEncoding(characterSet);
string requestMessage = encoding.GetString(arrayBuffer.Array, arrayBuffer.Offset, arrayBuffer.Count);
return requestMessage;
}
So, if you declare your contract something like:
[WebInvoke(Method = "POST", UriTemplate = "create", ResponseFormat=WebMessageFormat.Json)]
int CreateItem(Stream streamOfData);
(you can use XML instead)
The streamOfData should be the body of an HTTP POST. You can deserialize it using something like:
StreamReader reader = new StreamReader(streamId);
String res = reader.ReadToEnd();
NameValueCollection coll = HttpUtility.ParseQueryString(res);
It's working like that for us, at least. You may want to use a different approach to get the string into an XMLDocument or something. This works for our JSON posts. Might not be the most elegant solution, but it is working.
I hope this helps.
Glenn
Try this,
OperationContext.Current.RequestContext.RequestMessage
Here's how you do it without reflection:
using (var reader = OperationContext.Current.RequestContext.RequestMessage.GetReaderAtBodyContents ()) {
if (reader.Read ())
return new string (Encoding.ASCII.GetChars (reader.ReadContentAsBase64 ()));
return result;
}
}
If the reader is a HttpStreamXmlDictionaryReader (as it was in my case), the class's implementation of the method ReadContentAsBase64(byte[] buffer, int index, int count) simply passes these parameters to the Stream.Read method.
Once I have the byte[] I convert the bytes to a string via ASCII encoding. For a proper implementation, you could use the content type & encoding from the message's headers to do per HTTP spec.
You could arrest the HttpApplication.Request.InputStream in a custom HttpModule of the WCF Service, read the stream and again set its position to 0 in the custom HttpModule's event handler. Then store it in session and access it further in the actual OperationContract.
For example:
public class CustomModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.AcquireRequestState +=context_AcquireRequestState;
}
void context_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
Stream str = application.Request.InputStream;
StreamReader sr = new StreamReader(str);
string req = sr.ReadToEnd();
str.Position = 0;
application.Session["CurrentRequest"] = req;
}
}