Im trying to use this example from msdn on how to create a custom host for text template generation.
The CustomCmdLineHost class implements the ITextTemplatingEngineHost interface but not completely, the ResolveDirectiveProcessor is not implemented and it throws each time an exception wich is normal. Here is the ResolveDirectiveProcessor method:
public Type ResolveDirectiveProcessor(string processorName)
{
//This host will not resolve any specific processors.
//Check the processor name, and if it is the name of a processor the
//host wants to support, return the type of the processor.
//---------------------------------------------------------------------
if (string.Compare(processorName, "XYZ", StringComparison.OrdinalIgnoreCase) == 0)
{
//return typeof();
}
//This can be customized to search specific paths for the file
//or to search the GAC
//If the directive processor cannot be found, throw an error.
throw new Exception("Directive Processor not found");
}
and processorName passsed to this function is "T4VSHost",
The question now:
What is the type of "T4VSHost" to return in this method ?
P.S.: i tried "Microsoft.Data.Entity.Design.VisualStudio.Directives.FallbackT4VSHostProcessor" but it seems that it doesnt exist in any namespace.
It appears that the only way is to create that type. how ? by creating a class (lets call it FallbackT4VSHostProcessor) that inherits from the DirectiveProcessor abstract class which lives in the Microsoft.VisualStudio.TextTemplating namespace (the only example i found in the internet is Here). then we need to return the type of FallbackT4VSHostProcessor in the ResolveDirectiveProcessor like this:
Type ITextTemplatingEngineHost.ResolveDirectiveProcessor(string processorName)
{
if (string.Compare(processorName, "T4VSHost", StringComparison.OrdinalIgnoreCase) == 0)
{
return typeof(FallbackT4VSHostProcessor);
}
throw new Exception("Directive Processor not found");
}
I hope this will help someone someday.
Related
im new to C# and Tia Openness and have an problem. I dont know what parameter goes inside my ImportSingleTextList();.Its an example from Siemens but there is never mentioned how to call it inisde the main. That is my code.
private static void ImportSingleTextList(HmiTarget hmitarget)
{
TextListComposition textListsComposition = hmitarget.TextLists;
IList<TextList> importedTextLists = textListsComposition.Import(new FileInfo(#"D:\SamplesImport\myTextList.xml"), ImportOptions.Override);
}
I guess you have to look into your HmiTarget exactly. Is it a class, then you should instantiate a first instance of it; what constructor does this class have - with or without parameters? Click on HmiTarget and see what input it expects.
I guess you class has some kind of enumerable hmitarget.TextLists that you have to fill or get too.
Presumably you have a Project instance. You have to drill down from Project->Device->DeviceItem(->DeviceItem) until you find a DeviceItem that can provide a SoftwareContainer service. It may be that all such DeviceItems reside at the first level below Device; I haven't checked. Anyway, here's a method I wrote that searches the first and second DeviceItem levels:
public static HmiTarget GetHmiTarget(Device hmiDevice)
{
//search first level of DeviceItems
foreach (DeviceItem di in hmiDevice.DeviceItems)
{
SoftwareContainer container =
di.GetService<SoftwareContainer>();
if (container != null)
{
HmiTarget hmi = container.Software as HmiTarget;
if (hmi != null)
return hmi;
}
//search second level of DeviceItems
foreach (DeviceItem devItem in di.DeviceItems)
{
SoftwareContainer subContainer = devItem.GetService<SoftwareContainer>();
if(subContainer != null)
{
HmiTarget hmi = subContainer.Software as HmiTarget;
if (hmi != null)
return hmi;
}
}
}
return null; //nothing was found at the first or second levels
}
to get the Device, you can use PROJECT.Devices.Find(NAME) where PROJECT is your TIA portal project instance, and NAME is the string name of your HMI device.
I have one NuGet package that has code like this in it:
services.AddHttpClient("CompanyStandardClient").AddCompanyAuthenticationHeaders();
And another Nuget project with code like this in it:
services.AddHttpClient("CompanyStandardClient").AddCompanyHeaderPropagation();
Basically, one NuGet sets up my company's authentication, and another sets up the company's header propagation.
I usually would do this code like this:
services.AddHttpClient("CompanyStandardClient").AddCompanyAuthenticationHeaders().AddCompanyHeaderPropagation()
I am worried that if I do them separate, only one will be in effect. I looked at the code on GitHub and it returns a newed DefaultHttpClientBuilder for each call.
return new DefaultHttpClientBuilder(services, name);
But I am not sure if this means that the previous entry was overwritten.
Can the same named client be "added" separately? Or will it overwrite?
I think it can be done for the same named client based on the internal comments here.
// See comments on HttpClientMappingRegistry.
private static void ReserveClient(IHttpClientBuilder builder, Type type, string name, bool validateSingleType)
{
var registry = (HttpClientMappingRegistry)builder.Services.Single(sd => sd.ServiceType == typeof(HttpClientMappingRegistry)).ImplementationInstance;
Debug.Assert(registry != null);
// Check for same name registered to two types. This won't work because we rely on named options for the configuration.
if (registry.NamedClientRegistrations.TryGetValue(name, out Type otherType) &&
// Allow using the same name with multiple types in some cases (see callers).
validateSingleType &&
// Allow registering the same name twice to the same type.
type != otherType)
{
string message =
$"The HttpClient factory already has a registered client with the name '{name}', bound to the type '{otherType.FullName}'. " +
$"Client names are computed based on the type name without considering the namespace ('{otherType.Name}'). " +
$"Use an overload of AddHttpClient that accepts a string and provide a unique name to resolve the conflict.";
throw new InvalidOperationException(message);
}
if (validateSingleType)
{
registry.NamedClientRegistrations[name] = type;
}
}
Source
The client options configurations will aggregate to a single option.
Since you can only add pages to a FixedDocument, I wrote a derived class:
public class CustomFixedDocument : FixedDocument
{
public void RemoveChild(object child)
{
base.RemoveLogicalChild(child);
}
}
to replace FixedDocument, which works fine, until I try to print the document and receive the following error:
An unhandled exception of type
'System.Windows.Xps.XpsSerializationException' occurred in
ReachFramework.dll
Additional information: Serialization of this type of object is not
supported.
I haven't worked with serialization that much in the past and have read up on it but still can't solve the issues. I have also tried the
[Serializable]
attribute, but it doesn't make any difference.
Can anybody guide me in the correct direction or have any ideas what to do?
If you look at decompiled source code of the method which checks if certain type is supported, you will see roughly the following:
internal bool IsSerializedObjectTypeSupported(object serializedObject)
{
bool flag = false;
Type type = serializedObject.GetType();
if (this._isBatchMode)
{
if (typeof (Visual).IsAssignableFrom(type) && type != typeof (FixedPage))
flag = true;
}
else if (type == typeof (FixedDocumentSequence) || type == typeof (FixedDocument) || (type == typeof (FixedPage) || typeof (Visual).IsAssignableFrom(type)) || typeof (DocumentPaginator).IsAssignableFrom(type))
flag = true;
return flag;
}
Here you see that this type should either inherit DocumentPaginator, Visual, or be exactly of type FixedDocument, FixedDocumentSequence, FixedPage. So, types inherited from FixedDocument will not work, whatever serializable attributes you will use, so you have to find a different approach. I think that is a bug in XpsSerializationManager, but maybe there is some deep reason.
I decided to try the OP's approach and see if I can get it to work.
According to the snippet posted by Evk, although the IsSerializedObjectTypeSupported() function will not accept our own custom derivative of FixedDocument, it will accept a DocumentPaginator, and one of the overloads of XpsDocumentWriter.Write() accepts a paginator, so that should work, right?
Well, it turns out that if you do XpsDocumentWriter.Write( myFixedDocument.DocumentPaginator ) (where myFixedDocument is a custom derivative of FixedDocument) then something throws a NullReferenceException somewhere deep in library code. So, no luck there.
However, according to the same snippet, a FixedPage is also a supported type, and the XpsDocumentWriter.Write() method has another overload which accepts individual instances of FixedPage.
So, the following code worked for me:
foreach( FixedPage fixedPage in
fixedDocument.Pages.Select( pageContent => pageContent.Child ) )
xpsDocumentWriter.Write( fixedPage );
(Where Select() comes from using System.Linq)
i have developing project in c# for creating a user in AD.
i create a user and i want to create a attribute,like "mobilenumber"for this user.
when,i create this,the below error will occured.
here my code.
if (userDetails.GetUnderlyingObjectType() == typeof(DirectoryEntry))
{
dEntry = (DirectoryEntry)userDetails.GetUnderlyingObject();
if (User.UsrPassword != null && User.UsrPassword.Trim() != "")
{
if (dEntry.Properties.Contains("mobilenumber"))
{
Console.WriteLine("mobilenumberAttribute:Already created");
dEntry.Properties["mobilenumber"][0] = User.UsrPassword;
dEntry.CommitChanges();
}
else
{
Console.WriteLine("mobilenumber Attribute: Adding");
dEntry.Properties["mobilenumber"].Add(User.UsrPassword);
dEntry.CommitChanges();
}
userDetails.Save();
result = true;
}
}
The requested operation did not satisfy one or more constraints associated with the class of the object. (Exception from HRESULT: 0x80072014)
How can i resolve this?
Create an attribute? You mean like extending the schema? You can't do that by just adding it to an object. As you can see here, there is no such attribute as "mobilenumber". Maybe you want otherMobile (Phone-Mobile-Other) or mobile (Phone-Mobile-Primary)?
What are you trying to do? Why keep a copy of the password in the user object. If the user changes it, your copy will not be updated. If you need it to somehow inform the user, do something different like infoming his supervisor... Just a thought.
I'm working on a windows phone 7 application which uses Silverlight. What I'm looking to do is create a new instance of a class given a string containing the name of the correct class I would like to create. Below is the snipit of code I'm referring to and I am trying to use it to create the new instance. I know from debugging that serviceClass contains the correct string and if I add ".cs" to it, it would now correspond directly to one of the classes I have, so why isn't it being created?
WebViewService foundService; //From above
....
....
services.TryGetValue(mode, out foundService); //Find service
if (foundService == null)
{
string serviceClass;
serviceRegistry.TryGetValue(mode, out serviceClass); //Find serviceClass
if (serviceClass != null) //create new web service if one is found
{
try
{
//TODO: This is not working properly, should create an instance of some child class of WebViewService
foundService = (WebViewService)System.Activator.CreateInstance(Type.GetType(serviceClass+".cs"));
services.Add(mode, foundService);
}
catch
{
//But instead it always ends up here with ArgumentNullExeption
}
}
}
return foundService;
}
Any help at all or any suggestions would be greatly appreciated. Thanks for your time!
If your string contains fully qualified type name then you can create instance.
Try using the full name of the class (namespace included), without the ".cs" part. For example: YourApp.Services.YourWebViewService
Type.GetType doesn't take a string with a file name. It takes a class name or a struct name.